android - 如何为自定义 ExpandableListView 调用 notifyDataSetChanged?

标签 android expandablelistview expandablelistadapter

我正在尝试为我的自定义 ExpandableListView 调用 notifyDataSetChanged。我的子列表会发生什么,它显示所选文件夹(组列表)中的文件。单击子项时,它会询问是否删除文件。然后,删除后,ExpandableList 将“刷新”。但不知何故,我无法调用 notifyDataSetChanged 方法。

我的自定义适配器:

public class customListAdapter extends BaseExpandableListAdapter {
    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private File mapFolder = new File (Environment.getExternalStorageDirectory(),"/MLT/A");
    private File recordFolder = new File (Environment.getExternalStorageDirectory(),"/MLT/B");
    private File scribbleFolder = new File (Environment.getExternalStorageDirectory(),"/MLT/C");
    private String[] groups = { "A", "B", "C" };
    private String[][] children = { mapFolder.list(), recordFolder.list(), scribbleFolder.list() };

    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                                View convertView, ViewGroup parent) {

        TextView textView = new TextView(MLT_File.this);
        textView.setBackgroundColor(Color.BLACK);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(100, 5, 0, 5);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(23);
        textView.setId(1000);

        textView.setText(getChild(groupPosition, childPosition).toString());
        return textView;
    }//getChildView

    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    public int getGroupCount() {
        return groups.length;
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        TextView textView = new TextView(MLT_File.this);
        textView.setBackgroundColor(Color.WHITE);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(100, 0, 0, 0);
        textView.setTextColor(Color.BLACK);
        textView.setTextSize(25);
        textView.setText(getGroup(groupPosition).toString());

        return textView;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }

    public void notifyDataSetChanged() {
        this.notifyDataSetChanged();
    }

}//customListAdapter

我的 onCreate :

// Set up our adapter
    listAdapter = new customListAdapter();

    expLV = (ExpandableListView) findViewById(R.id.mlt_expList);
    expLV.setAdapter(listAdapter);
    //registerForContextMenu(expLV);

    expLV.setOnChildClickListener(new OnChildClickListener()
    {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) 
        {
            // TODO Auto-generated method stub
            String parentName = "";
            if (groupPosition == 0)
            {
                parentName = "A";
            }

            else if (groupPosition == 1)
            {
                parentName = "B";
            }

            else if (groupPosition == 2)
            {
                parentName = "C";
            }

            TextView childView = (TextView) v;
            String childName = childView.getText().toString();

            File file = new File(Environment.getExternalStorageDirectory(),"/Folder/"+childName);
            file.delete();

            return false;
        }
    });//OnChildClickListener

我的习惯中的 notifyDataSetChanged() 是我尝试做的,因为我意识到我的 ExpandableList 没有那个。但是,当我按 expLV.n 时,我创建的方法仍然没有出现 ...

最佳答案

实际上,有一个 notifyDataSetChanged() function on BaseExpandableListAdapter .

你一事无成:

 public void notifyDataSetChanged() {
    this.notifyDataSetChanged();
}

请删除它。

更改数据集后,从 OnItemClickListener 中的 onClick(View v) 调用 adapter.notifyDataSetChanged()

关于android - 如何为自定义 ExpandableListView 调用 notifyDataSetChanged?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8959722/

相关文章:

android - 如何在android中检测EditText中的表情符号

android - 可扩展的 ListView 按年/月对项目进行分组

java - 如何避免在键盘中单击“完成”时调用 ExpandableListView 中的 getChildView

android - 查找 View 的布局类型

java - Android SQLite 测验应用程序

android - 为什么只有最新的订阅者在使用 Kotlin 和 RxMVP 的 Android 上的 RxJava 中获得 onNext 事件

生成 XML 文件时出现 Android 错误

android - 来自 Json 的可扩展 ListView

android - 如何存储/保存 HashMap<String, List<String>>

Android - 可扩展的 ListView - 使用 ViewHolder 进行优化