ExpandableListView + BaseExpandableListAdapter:可以添加项目但不能清除列表

时间:2016-05-17 12:47:43

标签: android expandablelistview expandablelistadapter

我有ExpandableListView使用非常原始的BaseExpandableListAdapter实现。需要ArrayList并从该列表中获取数据:

public class ListAdapter extends BaseExpandableListAdapter {
    public ListAdapter (Context context, ArrayList<MyListItem> itemContainer) {
        m_inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        m_items = itemContainer;
    }

    @Override
    public int getGroupCount() {
        return m_items.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

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

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = m_inflater.inflate(R.layout.item_header, parent, false);
        }

        TextView label = (TextView) convertView.findViewById(R.id.itemHeader);
        label.setText(String.valueOf(m_items.get(groupPosition).version) + " (" + m_items.get(groupPosition).date + ")");

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = m_inflater.inflate(R.layout.list_item, parent, false);
        }

        TextView label = (TextView) convertView.findViewById(R.id.item_description);
        label.setText(m_items.get(groupPosition).description);

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    LayoutInflater m_inflater = null;
    ArrayList<MyListItem> m_items = null;
}

该活动持有&#34;原创&#34; ArrayList<MyListItem>并操纵它。添加项目时,它会执行以下操作:

 m_items.add(new ListItem(itemId, version, dateString, description));
 m_listAdapter.notifyDataSetChanged();

这很有效。但清除清单不会!

void clearList() {
    m_items.clear();
    m_listAdapter.notifyDataSetChanged();
}
当然,我已经检查过清除列表的代码实际上是被调用的。那么问题是什么?

1 个答案:

答案 0 :(得分:0)

在您的活动中执行清晰数据逻辑的方法中,添加:

public void clearList()
    {
        m_items.clear();
        m_listAdapter=new ListAdapter(this,m_items);
        <your_expandableListView_name>.setAdapter(m_listAdapter)
    }