1

Overview:

  • I have an Expandable List View which displays items of the type ToDoElement.
  • Each ToDoElement item has one String for the Group-Element and 1-3 Strings for the Child-Elements
  • My custom List Adapter expListAdapter gets the informations about the group- and child-items with the methods createGroupList() and createChildList().
  • In the Method createChildList() i check how many Strings for the child-items are in the ToDoElement and create the child-items for the Adapter.
  • At the end I can sort my list by comparing the Strings for the group-items.

All of this works fine, but here is my Problem:

  • After the items in the List View are sorted correctly, the number of child-items is wrong.
  • That's because my List Adapter doesn't know that the number of child-items has changed. So some child-items aren't displayed and other child-items are just empty, because there is no data for them.

My Suggestion:

I know that somehow the methods createGroupList() and createChildList() have to be called again from my Adapter, but i don't know how to do it.
I've tried expListAdapter.notifyDataSetInvalidated(), because somebody told me that this method would call createGroupList() and createChildList() again, but is doesn't work.

Code Fragments:

Definition of my List Adapter:

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

createGroupList() and createChildList():

    private List createGroupList() {    
      ArrayList result = new ArrayList();
      for (int i=0; i < Liste.AnzahlElemente(); i++) {    //
        result.add(Liste.getSize(i).getGroupString());
      }
      return result;
    }

    private List createChildList() {
        ArrayList result = new ArrayList();
        for(int i=0; i < Liste.getSize(); i++) {
            ArrayList secList = new ArrayList();
            for( int n = 1 ; n <= 3 ; n++ ) {
                if (Liste.getElement(i).getChildString(n).length() != 0){
                    secList.add( "- " + Liste.getElement(i).getChildString(n));
                }
            }
            result.add( secList );
        }
        return result;
}

Sorting the List:
(Liste is an ToDoListe object, which manages the ToDoElements in an Array List)

Collections.sort(Liste.getListe(), new NameComparator());

expListAdapter.notifyDataSetInvalidated();

//expListAdapter.notifyDataSetChanged(); //same effect as notifyDataSetInvalided

my NameComparator:

public class NameComparator implements Comparator<ToDoElement>{

     public int compare(ToDoElement item1, ToDoElement item2) {
           return item1.getGroupString().compareTo(item2.getGroupString());
     }
}


Thanks a lot for your help!!

OnClickListener
  • 190
  • 1
  • 3
  • 11

2 Answers2

3

I solved my own problem!

I wrote an Update method for the my custom List Adapter.

It looks like this:

public class MyListAdapter extends BaseExpandableListAdapter{

     private ArrayList<String> ueberschriften;
     private ArrayList<ArrayList<String>> stichpunkte;

     public MyListAdapter(Context context, List _ueberschriften, List _stichpunkte) { 

          this.ueberschriften = (ArrayList)_ueberschriften;
          this.stichpunkte = (ArrayList)_stichpunkte;

          inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

     // [...] some other methods

     public void update(List _GroupStrings, List _ChildStrings) {
          GroupStrings = (ArrayList<String>) _GroupStrings;
          ChildStrings = (ArrayList<ArrayList<String>>) _ChildStrings;
     }
}

Sorting the list in my activity looks like this:

Collections.sort(Liste.getListe(), new NameComparator());

expListAdapter.update(createGroupList(), createChildList());

expListAdapter.notifyDataSetChanged();


now the number of child-items also updates correctly!
cheers!

OnClickListener
  • 190
  • 1
  • 3
  • 11
0

The list in your Adapter and the list displayed are separate lists.

In case the adapter is filled with new data or data is rearanged, you need to inform the view with notifyDataSetChanged() - this will trigger the redrawing of the view.

notifyDataSetInvalidate() should only be called in case your data is not available any more.

As described here: Android ListView Adapter notifyDataSetInvalidated() vs notifyDataSetChanged()

Community
  • 1
  • 1
Drejc
  • 13,466
  • 15
  • 65
  • 101
  • for me there's no difference in using `notifyDataSetChanged()` or `notifyDataSetInvalidate()`. the list still looks the same after sorting. – OnClickListener Feb 14 '12 at 13:28
  • I solved the problem by writing an Update Method for my custom List Adapter! I will post the code as soon as possible! – OnClickListener Feb 14 '12 at 14:51