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 8417417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:02:32+00:00 2026-06-10T02:02:32+00:00

I have an application that has 2 screens. The first screen has a ListView

  • 0

I have an application that has 2 screens. The first screen has a ListView of movies with a row consisting of 3 Elements: Title, Date and Gross declared in strings.xml. The user has the option of adding a movie by clicking the menu button, which sends him to another screen. The second screen has 3 Edit texts that correspond to Title Date and Gross, which is alphabetically sorted straight away when it returns to screen 1.

Similarly, the user can also Edit/Delete entries by long clicking a row thatbrings up a context menu. The Edit function works like this:

a.) User long clicks Titanic and chooses Edit
b.) Row gets deleted, and user is brought to screen 2
c.) Edit texts are populated with the initial data from the deleted Row
d.) When user edits data, new movie is added at the bottom of the ListView.

The problem arises when the user deletes this new movie at the bottom of the ListView. Logcat gives a

   java.lang.IndexOutOfBoundsException: Invalid index 50, size is 50

Here is my code (Take note I am using Perst to persist data, but I don;t think that won’t really matter with my problem):

       case R.id.contextedit:
            Lab9_082588FetchDetails row = (Lab9_082588FetchDetails)    getListView()
                    .getItemAtPosition(info.position);
            Intent editData = new Intent(MovieList.this,    Lab9_082588Edit.class);
        String startTitle = row.getTitle();
        String startGross = row.getGross();
        String startDate = row.getDate();

        editData.putExtra(Lab9_082588Edit.TITLE_STRING, startTitle);
        editData.putExtra(Lab9_082588Edit.GROSS_STRING, startGross);
        editData.putExtra(Lab9_082588Edit.DATE_STRING, startDate);
        startActivityForResult(editData, MovieList.EDIT_MOVIE);

        int posEdit = info.position;
        String editTitle = results.get(info.position).getTitle();
        results.remove(posEdit);
        adapter.notifyDataSetChanged();

                    //Perst
        Index<Lab9_082588FetchDetails> rootEdit = (Index<Lab9_082588FetchDetails>) db
                .getRoot();
        rootEdit.remove(editTitle, results.get((int) info.id));
        db.setRoot(rootEdit);

        return true;

Edit Class:

       @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection using item.getItemId()
    switch (item.getItemId()) {
    case R.id.edit:
        next();
        break;
    }
    return true;
}

private void next() {
    // TODO Auto-generated method stub
    EditText movieTitle = (EditText) findViewById(R.id.etTitle);
    EditText movieGross = (EditText) findViewById(R.id.etGross);
    EditText movieDate = (EditText) findViewById(R.id.etDate);

    String title = movieTitle.getText().toString();
    String gross = movieGross.getText().toString();
    String date = movieDate.getText().toString();

    if ((title.length() > 0) && (gross.length() > 0)
            && (date.length() == 4)) {

        Intent hobby = getIntent();
        hobby.putExtra(Lab9_082588Edit.TITLE_STRING, title);
        hobby.putExtra(Lab9_082588Edit.GROSS_STRING, gross);
        hobby.putExtra(Lab9_082588Edit.DATE_STRING, date);
        setResult(RESULT_OK, hobby);
        finish();
    }
}

Delete function:

             int posDelete = info.position;
                                String deleteTitle = results.get(
                                        info.position).getTitle();
                                results.remove(posDelete);
                                adapter.notifyDataSetChanged();

                                  Index<Lab9_082588FetchDetails> rootDelete = (Index<Lab9_082588FetchDetails>) db
                                        .getRoot();
                                rootDelete.remove(deleteTitle,
                                        results.get(info.position));
                                db.setRoot(rootDelete); //Perst

                                return;

OnActivityResult (Edit):

         case EDIT_MOVIE:
            Lab9_082588FetchDetails edittedMovie = new Lab9_082588FetchDetails();
            NumberFormat formatterEdit = new DecimalFormat("###,###,###");

            edittedMovie.setTitle(data
                    .getStringExtra(Lab9_082588Add.TITLE_STRING));
            edittedMovie.setGross("$"
                    + formatterEdit.format(Double.parseDouble(data
                            .getStringExtra(Lab9_082588Add.GROSS_STRING))));
            edittedMovie.setDate(data
                    .getStringExtra(Lab9_082588Add.DATE_STRING));

            results.add(edittedMovie);
            adapter.notifyDataSetChanged();

Populating the Listview:

  for (int i = 0; i < items.size(); i++) {
        Lab9_082588FetchDetails sr = new Lab9_082588FetchDetails();
        sr.setTitle(items.get(i).getTitle());
        sr.setGross(items.get(i).getGross());
        sr.setDate(items.get(i).getDate());
        results.add(sr);
        Collections.sort(results, ignoreCaseStart);
    }

How do I remedy this?

  • 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-06-10T02:02:33+00:00Added an answer on June 10, 2026 at 2:02 am

    This problem occurs because in your delete function, you first remove the element from the results collection(“results.remove(posDelete);”), and then, a few lines later, you call “results.get(info.position)” to fetch a parameter for the rootDelete.remove call, but which is already removed.

    If the element is the last element of your collection, let’s say the 50th element, the value for “info.position” is 50. You remove one element, so the number of elements is now 49. In the rootDelete.remove line you call results.get(50), which produces the error.

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

Sidebar

Related Questions

In my application I have several activities, the main screen has 4 buttons that
I have an application that has two threads. The first one (the main thread)
I have an application that has 2 beans with the same name, but which
We have an application that has a database full of polygons (currently stored as
I have a application that has an activity that shows message logs. The thing
I have an application that has two localization options at the moment through .resx
i have an application that has a dependancy on gdiplus. i need the application
I have an application that has a list of buttons and has customized tooltips.
I have an application that has a .sql file in it. The sql file
We have an application that has a WCF service (*.svc) running on IIS7 and

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.