First a short overview of my code, i hope its understandable:
ToDoElement
with a few variables.ToDoListe
which manages the ToDoElement
s in an ArrayList.ToDoListe
contains the method deleteElement()
to delete a ToDoElement
from the ArrayListliste_activity
i create an object of ToDoListe
and fill it with some Objects of ToDoElement
.expListAdapter
.ToDoElement
s.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!!
You should call expListAdapter.notifyDataSetInvalidated()
expListAdapter.notifyDataSetChanged()
will just update your views for the new values for each items (no change in any of the elements)
i.e it will again call your getChildView
and getGroupView
for each item.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With