1

我正在学习 Android/Java,并且我有一个ListView附加到数据库,并且simple_list_item_multiple_choice在每一行中都包含一个复选框。当用户选中 1 个复选框并点击删除按钮时,该行将被删除并相应ListView更新。但是,如果用户检查多行,我的应用程序就会中断。

为什么我的应用程序在选择ListView要从附加数据库中删除的多行时会中断?

public class ListViewDisplayNumbers extends ListActivity {
public void DeleteNumber() {

    NumbersDataSource datasource = new NumbersDataSource(this);
    datasource.open();

    ListView listView = (ListView) findViewById(android.R.id.numberlist);
    SparseBooleanArray checked = listView.getCheckedItemPositions();

    List<Number> values = datasource.getAllNumbers();
    ArrayAdapter<Number> adapter = new ArrayAdapter<Number>(this, android.R.layout.simple_list_item_multiple_choice, values);

    for (int i = 0; i < checked.size(); i++) {
        int position = checked.keyAt(i);
        if (checked.valueAt(i))
            datasource.deleteNumber(adapter.getItem(position));
            adapter.remove(adapter.getItem(position)); //breaks on the last one of a multiple
    }

    adapter.notifyDataSetChanged();
    setListAdapter(adapter);
    datasource.close();
}

日志猫:

01-25 12:26:06.236: E/AndroidRuntime(7140): FATAL EXCEPTION: main
01-25 12:26:06.236: E/AndroidRuntime(7140): java.lang.IndexOutOfBoundsException: Invalid index 3, size is 2
01-25 12:26:06.236: E/AndroidRuntime(7140):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at java.util.ArrayList.get(ArrayList.java:304)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.example.hw.ListViewDisplayNumbers.deleteNumber(ListViewDisplayNumbers.java:103)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.example.hw.ListViewDisplayNumbers.onOptionsItemSelected(ListViewDisplayNumbers.java:128)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.app.Activity.onMenuItemSelected(Activity.java:2507)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:982)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:175)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.widget.AdapterView.performItemClick(AdapterView.java:292)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.widget.AbsListView.performItemClick(AbsListView.java:1393)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:3022)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.widget.AbsListView$1.run(AbsListView.java:3817)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.os.Handler.handleCallback(Handler.java:605)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.os.Looper.loop(Looper.java:137)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at android.app.ActivityThread.main(ActivityThread.java:4517)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at java.lang.reflect.Method.invoke(Method.java:511)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-25 12:26:06.236: E/AndroidRuntime(7140):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

尝试向后执行此操作

for (int i = checked.size()-1; i >=0;i--) {
    int position = checked.keyAt(i);
    if (checked.valueAt(i)){
        datasource.deleteNumber(adapter.getItem(position));
        adapter.remove(adapter.getItem(position)); //breaks on the last one of a multiple
     }
}

理由:有一个类似的问题,遇到了这个建议倒退的问题。不过这是有道理的,因为如果您前进,删除可能会弄乱索引,而如果您后退,则在修改列表时不应影响任何内容。

哦,我注意到您可能希望将两个适配器语句放在一个if块中。

于 2013-01-25T20:35:20.423 回答