Android: IndexOutOfBounds Error when deleting row in ListView

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?


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.

链接地址: http://www.djcxy.com/p/93164.html

上一篇: 水平RecyclerView内部的水平ViewPager

下一篇: Android:IndexOutOfBounds在ListView中删除行时出错