在Adapter中以编程方式刷新列表视图

时间:2016-07-21 07:35:26

标签: android listview adapter expandablelistview baseadapter

如何在Expandable List View

刷新 儿童列表

我使用以下代码来删除子列表,但unable to refresh子项目列表。

public class ListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader;
private HashMap<String, List<String>> _listDataChild;
private DBAdapter mydb;

    public SyncExpandablePendingListAdapter(Context context, List<String> listDataHeader,
                                        HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

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

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

    ....

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.sync_pending_list_item, null);
    }

    ....

    btnDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

                Log.d("oldList:", _listDataChild.toString());

                mydb.DeleteECRecord(data2);
                _listDataChild.remove(childPosition);
                notifyDataSetChanged();

                Log.d("newList:", _listDataChild.toString());

    });

}

.............

}

实际上,我没有得到我必须做出改变的地方?究竟我错过了什么? :

请让我知道我在做什么错误?我错过了什么?

2 个答案:

答案 0 :(得分:2)

您从数据库中删除了该条目,但未将其从HashMap中删除。也可以从HashMap中删除它,然后调用notifyDataSetChanged()。

答案 1 :(得分:0)

在调用notifyDataSetChanged()之前,必须重新加载支持适配器的列表。 notifyDataSetChanged()让适配器知道列表已更改,从而重新加载列表。但是您必须加载适配器的支持列表以显示视图。