First a short overview of my code, i hope its understandable:
- I have a class called
ToDoElement
with a few variables. - I have another class called
ToDoListe
which manages theToDoElement
s in an ArrayList. ToDoListe
contains the methoddeleteElement()
to delete aToDoElement
from the ArrayList- In my main activity
liste_activity
i create an object ofToDoListe
and fill it with some Objects ofToDoElement
. - In the same activity i have an ExpandableListView with my own Adapter called
expListAdapter
. - The Adapter creates the Groupviews and Childviews by getting the String variables of the
ToDoElement
s. - I created a ContextMenu for every Group item of the list, in which i use the method
deleteElement()
.
Ok, now here is my problem:
After i used the method deleteElement()
i want to update my List, because the data of the ArrayList in ToDoListe
changed. So i call expListAdapter.notifyDataSetChanged()
.
But then my whole activity crashes, with the reason: "IndexOutOfBoundException: Invalid index 4, size is 4" (I had 5 ToDoELement
items in my list, before deleting one of them).
I know that's because of one of my for-loops, but i don't have any idea why.
Code fragments:
creating new Object of ToDoListe:
private static ToDoListe Liste = new ToDoListe();
class ToDoListe (just the 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 list Adapter:
expListAdapter = new MyListAdapter(this, createGroupList(), createChildList());
setListAdapter( expListAdapter );
create ArrayLists for list Adapter:
// 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 (just the 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;
}
use of notifyDataSetChanged():
Liste.deleteElement(groupPos);
expListAdapter.notifyDataSetChanged();
Thanks a lot for your attention!!