Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7518119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:39:56+00:00 2026-05-30T01:39:56+00:00

First a short overview of my code, i hope its understandable: I have a

  • 0

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 the ToDoElements in an ArrayList.
  • ToDoListe contains the method deleteElement() to delete a ToDoElement from the ArrayList
  • In my main activity liste_activity i create an object of ToDoListe and fill it with some Objects of ToDoElement.
  • 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 ToDoElements.
  • 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!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:39:57+00:00Added an answer on May 30, 2026 at 1:39 am

    You need to add a DeleteMethod of some sort to the Adapter and remove the item from the Adapter manually, not only remove it from the List.

    Each time you refresh the the views using notifyDataSetChanged() the Adapter will call the loop around the List. Your List in the Adapter gets a null value due to the changes you made.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an example class containing two data points: public enum Sort { First,
This short piece of code displays the date of the first day of the
I have two activities. The first activity display list of the users with short
I have a file whose first two lines look like this: price spec.long spec.short
First question here: it is a very short yet fundamental thing in Java that
Short: how does modelbinding pass objects from view to controller? Long: First, based on
I'm working on my first Django application. In short, what it needs to do
When I'm about to write a short algorithm, I first check in the base
this my first shot at this awesome new (to me) programmers site, I hope
Sitation: overview: I have something like this: std::vector<SomeType> values; std::vector<int> indexes; struct Range{ int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.