Android notifyDataSetChanged()不起作用

时间:2016-01-26 20:06:35

标签: android adapter expandablelistview

我已经阅读了很多有关此问题的主题,但我找不到解决方案。

myAdapter.notifyDataSetChanged();我的ExpandableListView的(BaseExpandableListAdapter)不会更新它的内置方法:getChildView()。

提前致谢!

代码: 这是我的适配器类:

public class MyAdapter extends BaseExpandableListAdapter {
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    String title = (String) this.getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if ( groupViewType[groupPosition] == 0) {
                Log.d("MyAdapter", "getChildView: setting child_layout");

                convertView = layoutInflater.inflate(R.layout.child_layout, null);

            } else {
                Log.d("MyAdapter", "getChildView: setting exercise_layout");

                convertView = layoutInflater.inflate(R.layout.exercise_layout, null);

            }
    }
    return convertView;
}

   // To alternate between layouts when swiping to the right or left.
public void updateView(int groupPosition) {

    if (groupViewType[groupPosition] == 1) {
        Log.d("MyAdapter", "UpdateView: from 1 to 0");
        groupViewType[groupPosition] = 0;
    }
    else {
        Log.d("MyAdapter", "UpdateView: from 0 to 1");
        groupViewType[groupPosition] = 1;
    }
}

这是我的MainActivity类,我在其中调用MyAdapter:

expandableListView = (ExpandableListView) findViewById(R.id.expListView);
final MyAdapter myAdapter = new MyAdapter(this,  Headings, ChildList);
expandableListView.setAdapter(myAdapter);

expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            myAdapter.updateView(groupPosition); // This method works fine!
            myAdapter.notifyDataSetChanged();  // It doesn't change the view in getChildView();
            return false;
        }
    });

解: 我通过设置convertView = null解决了这个问题;在getChildView()中的if语句之前。

很抱歉。

1 个答案:

答案 0 :(得分:0)

您必须使用ViewHolder模式才能正常工作