我正在尝试从sq-lite数据库中删除数据,数据在带有复选框的自定义列表视图中加载。当用户选中某个复选框然后按删除按钮时,它应删除所有检查的数据。
一切正常,它正在删除所有已检查的数据。但问题出现在最后两个数据上。当剩下最后2个数据时,如果我选择两个数据进行删除则会出错。
Logcat位于
之下12-16 12:55:51.049: E/AndroidRuntime(11489): FATAL EXCEPTION: main
12-16 12:55:51.049: E/AndroidRuntime(11489): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
12-16 12:55:51.049: E/AndroidRuntime(11489): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
12-16 12:55:51.049: E/AndroidRuntime(11489): at java.util.ArrayList.get(ArrayList.java:304)
12-16 12:55:51.049: E/AndroidRuntime(11489): at iqualtech.skirr.Classes$1$1.onClick(Classes.java:98)
12-16 12:55:51.049: E/AndroidRuntime(11489): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:169)
12-16 12:55:51.049: E/AndroidRuntime(11489): at android.os.Handler.dispatchMessage(Handler.java:99)
12-16 12:55:51.049: E/AndroidRuntime(11489): at android.os.Looper.loop(Looper.java:154)
12-16 12:55:51.049: E/AndroidRuntime(11489): at android.app.ActivityThread.main(ActivityThread.java:4624)
12-16 12:55:51.049: E/AndroidRuntime(11489): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 12:55:51.049: E/AndroidRuntime(11489): at java.lang.reflect.Method.invoke(Method.java:511)
12-16 12:55:51.049: E/AndroidRuntime(11489): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
12-16 12:55:51.049: E/AndroidRuntime(11489): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
12-16 12:55:51.049: E/AndroidRuntime(11489): at dalvik.system.NativeStart.main(Native Method)
我的删除功能如下
private void deleteMenuSpinner() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
Classes.this);
final Spinner spinnerDelete = new Spinner(Classes.this);
alertDialog.setView(spinnerDelete);
adapterSpinner = ArrayAdapter.createFromResource(Classes.this,
R.array.delete_menu,
android.R.layout.simple_spinner_item);
adapterSpinner
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDelete.setAdapter(adapterSpinner);
alertDialog.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Code.i = true;
int len = mListView.getCount();
SparseBooleanArray checked = mListView
.getCheckedItemPositions();
for (int i = 0; i < len; i++)
if (checked.get(i)) {
Code.i = false;
String[] delete = names2.get(i);
String idString = delete[0];
long idLong = Long.valueOf(idString);
Log.d("Deleting...", idLong + "");
dataManipulator.delete(idLong);
names2.remove(i);
}
if (Code.i == true) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
Classes.this);
alertDialogBuilder.setTitle("No Data");
alertDialogBuilder
.setMessage(
"No Data Available to Delete")
.setCancelable(false)
.setPositiveButton(
"Ok",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
}
});
AlertDialog alertDialog = alertDialogBuilder
.create();
alertDialog.show();
} else {
names2 = dataManipulator.selectAll();
stg1 = new String[names2.size()];
int x = 0;
String stg;
for (String[] name : names2) {
stg = "Class Name : " + name[1];
stg1[x] = stg;
x++;
}
adapter = new ArrayAdapter<String>(
Classes.this,
R.layout.custom_list_item_multiple_choice,
stg1);
mListView
.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mListView
.setBackgroundResource(R.drawable.assignmentheader);
mListView
.setCacheColorHint(Color.TRANSPARENT);
mListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
});
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
alertDialog.show();
}
});
在下面一行给出错误
String[] delete = names2.get(i);
答案 0 :(得分:0)
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
在下面一行给出错误
String[] delete = names2.get(i);
names2
arraylist只有一个元素(大小为1),但你试图get()
第二个(索引1)。
代码未显示您设置names2
列表的方式,但无论如何len
索引循环的i
约束来自mListView.getCount()
,这可能不正确