Updating ExpandableListView with notifyDataSetChanged ()

First, a short overview of my code, I hope it is clear:

  • I have a class called ToDoElement with several variables.
  • I have another class called ToDoListe that manages ToDoElement in an ArrayList.
  • ToDoListe contains the deleteElement() method to remove a ToDoElement from an ArrayList
  • In my main activity, liste_activity I create a ToDoListe object and populate it with some ToDoElement objects.
  • In the same activity, I have an ExpandableListView with my own adapter called expListAdapter .
  • The adapter creates Groupviews and Childviews, receiving String ToDoElement s variables.
  • I created a ContextMenu for each Group element in the list in which I use the deleteElement() method.

Ok now here is my problem :

After I used the deleteElement() method, I want to update my list because the ArrayList data in ToDoListe changed. So I'm calling expListAdapter.notifyDataSetChanged() .
But then all my activity falls with the error: "IndexOutOfBoundException: Invalid index 4, size 4" (I had 5 ToDoElement elements in my list before deleting one of them). I know that because of one of my for-loops, but I don't know why.

Code snippets:

creating a new ToDoListe object:

 private static ToDoListe Liste = new ToDoListe(); 

ToDoListe class (only important methods):

 public class ToDoListe { private ArrayList<ToDoElement> ToDoListe; public ToDoListe() { ToDoListe = new ArrayList<ToDoElement>(); } public void newElement(ToDoElement element){ ToDoListe.add(element); } public void deleteElement(int nummer) { ToDoListe.remove(nummer); } public int AnzahlElemente() { return ToDoListe.size(); } } 

Define adapter list:

 expListAdapter = new MyListAdapter(this, createGroupList(), createChildList()); setListAdapter( expListAdapter ); 

create ArrayLists for the Adapter list:

 // creates the list with the group items private List createGroupList() { ArrayList result = new ArrayList(); for (int i=0; i < Liste.AnzahlElemente(); i++) { result.add(Liste.getElement(i).getUeberschrift()); } return result; } // creates the list with the child items private List createChildList() { ArrayList result = new ArrayList(); for(int i=0; i < Liste.AnzahlElemente(); i++) { ArrayList secList = new ArrayList(); for( int n = 1 ; n <= 3 ; n++ ) { if (Liste.getElement(i).getStichpunkt(n).length() != 0){ secList.add( "- " + Liste.getElement(i).getStichpunkt(n)); } } result.add( secList ); } return result; } 

my own list adapter (only important methods):

 public class MyListAdapter extends BaseExpandableListAdapter{ private ArrayList<String> ueberschriften; private ArrayList<ArrayList<String>> stichpunkte; private LayoutInflater inflater; public MyListAdapter(Context context, List _ueberschriften, List _stichpunkte) { this.ueberschriften = (ArrayList)_ueberschriften; this.stichpunkte = (ArrayList)_stichpunkte; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.item_child, null); } TextView tv = (TextView) convertView.findViewById(R.id.item_child_title); tv.setText(liste_activity.getListe().getElement(groupPosition).getStichpunkt(childPosition)); return convertView; } public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.item_group, null); } TextView tv = (TextView)convertView.findViewById(R.id.item_group_title); tv.setText(liste_activity.getListe().getElement(groupPosition).getUeberschrift()); return convertView; } 

using notifyDataSetChanged ():

 Liste.deleteElement(groupPos); expListAdapter.notifyDataSetChanged(); 


Thank you very much for your attention!

+8
android expandablelistview
source share
3 answers

You need to add some kind of DeleteMethod to the Adapter and remove the item from the Adapter manually, and not just remove it from the List .

Each time you refresh views with notifyDataSetChanged() , the Adapter calls a loop around the List . Your List in the Adapter gets a null value due to your changes.

+4
source share

The super call in your register DataSetObserver meke notifyDataSetChanged () works well.

 @Override public void registerDataSetObserver(DataSetObserver observer) { super.registerDataSetObserver(observer); } 
+4
source share

You must call expListAdapter.notifyDataSetInvalidated()

expListAdapter.notifyDataSetChanged() will just update your views for new values ​​for each element (no change in any of the elements)

ie will call your getChildView and getGroupView for each element.

0
source share

All Articles