Oreo
Oreo

Reputation: 2594

Refresh List View programmatically within Adapter

How to refresh Child List within Expandable List View

I am using following code to delete child list item, but unable to refresh Child Item List.

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());

    });

}

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

}

Actually, I am not getting where I have to make the change ? What exactly I have missed ? :

Please let me know Where I am doing MISTAKE ? What I am MISSING ?

Upvotes: 1

Views: 3706

Answers (2)

berserk
berserk

Reputation: 2728

You removed that entry from your database, but didn't remove it from the HashMap. Remove it from HashMap too and then call notifyDataSetChanged().

Upvotes: 2

ucsunil
ucsunil

Reputation: 7494

Before you call the notifyDataSetChanged(), you have to reload the list backing your adapter. notifyDataSetChanged() lets the adapter know that the list has changed and hence to reload the list. But you have to load the backing list for the adapter for the list to show the views.

Upvotes: 0

Related Questions