ExpandableListAdapter onClickListener, call notifyDataSetChanged() ExpandableListAdapter onClickListener, call notifyDataSetChanged() android android

ExpandableListAdapter onClickListener, call notifyDataSetChanged()


As beworker mentioned, your problem comes from using wrong data structure and here is why your app crashes. You are using header titles as keys to your hashmap and when you modify a title in listDataHeader the modification is correctly applies to it but not to the key object inside the hashmap. Meaning that your children still exist under the old title key inside the hashmap and the new title key has null as its value. Thus you will get a NullPointerException when you call the

this.listDataChild.get(this.listDataHeader.get(groupPosition)).get(childPosition);

since

this.listDataChild.get(this.listDataHeader.get(groupPosition))

returns Null

Solution would be using ArrayList for header titles and ArrayList> for children. The key in both of these arrayLists is your group index.


I believe the issue is in the data structures you use. You keep list of children assigned to the parent's name in a hash table. When parent's name changes in the list of parents, old children assignment in the hash table is not valid anymore.

I would suggest you to use SparseArray<List<String>> or List<List<String>> for listDataChild field. It will map group position to the list of children directly. Thus changing parent's name in listDataHeader wont break data consistency, as the group position will stay the same. Now you should be able to safely use notifyDataSetChanged() method to update headers.

Hope this helps.


Instead of using notifyDataSetChanged(); create an interface from where data is coming and implement it is your adapter and then change the value of the parent in that interface function, as getChildView() is called after every child it will update your listview Parent. Checked and Verified as I have same problem

Say you want to transfer data from Adapter to Adapter

adapter.java

public interface data { public void dataMethod(String data); }

and implement this method in adapter

public void dataMethod(String data) { this.data = data; }

call dataMethod() from getChildView() and update string as I have shown

and set data in getViewChild method in adapter