Android:删除ListView中的行时出现IndexOutOfBounds错误 [英] Android: IndexOutOfBounds Error when deleting row in ListView

查看:89
本文介绍了Android:删除ListView中的行时出现IndexOutOfBounds错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有2个屏幕的应用程序.第一个屏幕具有电影的ListView,该电影的行包含3个元素,分别在strings.xml中声明了Title,Date和Gross.用户可以选择通过单击菜单按钮添加电影,这会将他发送到另一个屏幕.第二个屏幕上有3个与标题日期"和总价"相对应的编辑"文本,当返回到屏幕1时,它们按字母顺序立即排序.

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.)用户长按泰坦尼克号",然后选择编辑" b.)行被删除,并且用户被带到屏幕2 c.)编辑文本中填充了已删除行中的初始数据 d.)用户编辑数据时,新影片将添加到ListView的底部.

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.

当用户在ListView底部删除此新电影时,就会出现问题. Logcat给出了

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

这是我的代码(请注意,我正在使用Perst来保存数据,但是我不认为这与我的问题无关紧要):

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;

编辑类:

       @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();
    }
}

删除功能:

             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(编辑):

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();

填充列表视图:

  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);
    }

我该如何补救?

推荐答案

之所以会出现此问题,是因为在删除函数中,您首先从结果集合中删除了该元素("results.remove(posDelete);"),然后,几行后,您调用"results.get(info.position)"以获取rootDelete.remove调用的参数,但该参数已被删除.

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.

如果该元素是集合中的最后一个元素,那么假设第50个元素,则"info.position"的值为50.您删除了一个元素,因此元素的数量现在为49.在rootDelete.remove中您调用result.get(50)的行,就会产生错误.

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.

这篇关于Android:删除ListView中的行时出现IndexOutOfBounds错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆