我正在学习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();
}
Log cat:
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)
答案 0 :(得分: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
}
}
理由:有一个类似的问题并遇到了this所以建议向后做的问题。这有意义,因为如果你前进,删除可能会搞砸索引,而如果你倒退,你修改列表时不应该影响任何东西。
哦,我注意到你可能想把两个适配器语句放在一个if
块中。