【问题标题】:how to call notifyDataSetChanged for custom ExpandableListView?如何为自定义 ExpandableListView 调用 notifyDataSetChanged?
【发布时间】:2012-02-16 02:23:10
【问题描述】:

我正在尝试为我的自定义 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 ...

时,我创建的方法仍然没有出现

【问题讨论】:

    标签: android expandablelistview expandablelistadapter


    【解决方案1】:

    其实有一个notifyDataSetChanged() function on BaseExpandableListAdapter

    你一事无成:

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

    请删除它。

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

    【讨论】:

    • 但我调用的是 onChildClickListener() 而不是 onItemClickListener。我不确定出了什么问题,但我就是无法调用 adapter.notifyDataSetChanged。只有 notify 和 notifyall
    • 你在哪里声明了你的适配器?它是一个类变量吗?发布您的 onClick 方法...
    • 我使用你的代码 public void notifyDataSetChanged(){ this.notifyDataSetChanged(); } 我得到一个 stackoverflowerror 有什么想法吗?
    【解决方案2】:

    我知道这已经过时了,但对于那些仍然遇到此问题的人,您可以使用 ExpandableListeView 中的 invalidateViews(),如提到的 here

    【讨论】:

      【解决方案3】:

      嗯...因为没有人真正告诉我该怎么做,所以我想到了一种可能很愚蠢但有用的方法。

      我所做的不是在 onCreate 中创建它,而是在 onResume() 中创建它。当我对其进行任何更改时,它将再次创建。这样做的原因是因为我正在使用 TabMenu,当我在它们之间切换时,该类不会再次创建,因此导致列表处于相同状态。

      通过使用 onResume,它还简化了我的工作,因为当我希望它刷新时,我所要做的就是调用 onResume()。

      希望这对其他人有所帮助。 =)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多