我使用以下代码从列表视图中获取所选项目的列表(从SQLite数据库填充)。对话框成功显示列表,一切正常。
private String getCheckedItems() {
SparseBooleanArray positions = getListView().getCheckedItemPositions();
StringBuilder sb = new StringBuilder();
for (int i = (positions.size() - 1); i >= 0; i--) {
Cursor cursor = (Cursor) getListView().getItemAtPosition(positions.keyAt(i));
if(positions.valueAt(i) == true)
sb.append(Utility.decodeContent("- " + cursor.getString(cursor.getColumnIndex("word")) + "\n"));
}
AlertDialog.Builder builder = new AlertDialog.Builder(
FavBawords2.this);
builder.setTitle((getResources().getString(R.string.HisFav_del_one)));
builder.setIcon(R.drawable.clear);
builder.setMessage(sb);
builder.setPositiveButton((R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
onPositiveButtonClicked();
}
});
builder.setNegativeButton((R.string.cancel_action), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do something else
}
});
builder.show();
return null;
}
我想从列表视图和数据库中选择一个或多个要删除的项目。以下是删除所选项目的代码:
protected void onPositiveButtonClicked() {
SparseBooleanArray positions = getListView().getCheckedItemPositions();
for(int i = 0; i < getListView().getCount(); i++){
Cursor cursor = (Cursor) getListView().getItemAtPosition(positions.keyAt(i));
String str = cursor.getString(cursor.getColumnIndex("word"));
if(positions.valueAt(i) == true) {
if(cursor.moveToFirst()) {
//favitems is my table
db.execSQL("delete from favitems WHERE word ='"+str+"'");
}
}
}
}
可悲的是,我收到ArrayIndexOutOfBoundsException
错误,如下所示,但实际上已删除所选项目。
06-08 18:01:19.587: W/dalvikvm(18952): threadid=1: uncaught exception occurred
06-08 18:01:19.588: W/System.err(18952): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
06-08 18:01:19.589: W/System.err(18952): at android.util.SparseBooleanArray.keyAt(SparseBooleanArray.java:176)
06-08 18:01:19.589: W/System.err(18952): at the_app.activity.FavBawords2$ModeCallback.onPositiveButtonClicked(FavBawords2.java:881)
06-08 18:01:19.589: W/System.err(18952): at the_app.activity.FavBawords2$ModeCallback$1.onClick(FavBawords2.java:844)
06-08 18:01:19.589: W/System.err(18952): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
06-08 18:01:19.589: W/System.err(18952): at android.os.Handler.dispatchMessage(Handler.java:110)
06-08 18:01:19.589: W/System.err(18952): at android.os.Looper.loop(Looper.java:193)
06-08 18:01:19.589: W/System.err(18952): at android.app.ActivityThread.main(ActivityThread.java:5292)
06-08 18:01:19.589: W/System.err(18952): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 18:01:19.589: W/System.err(18952): at java.lang.reflect.Method.invoke(Method.java:515)
06-08 18:01:19.589: W/System.err(18952): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
06-08 18:01:19.590: W/System.err(18952): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
06-08 18:01:19.590: W/System.err(18952): at dalvik.system.NativeStart.main(Native Method)
06-08 18:01:19.590: W/dalvikvm(18952): threadid=1: calling UncaughtExceptionHandler
06-08 18:01:19.591: E/AndroidRuntime(18952): FATAL EXCEPTION: main
我想问题可能在于这一行:
for(int i = 0; i < getListView().getCount(); i++)
因为当我把它改成
时 for (int i = (positions.size() - 1); i >= 0; i--)
我不再有问题了。但删除失败。 我想知道你是否可以帮助解释为什么我遇到这样的问题并向我展示出一条出路。
非常感谢。
答案 0 :(得分:1)
你是对的问题只在这一行。
for(int i = 0; i < getListView().getCount(); i++)
为什么:因为您使用的是ListView的总项目大小,而您可能只选择了少数几个。这是ArrayIndexOutOfBoundsException
像这样写
delete from favitems WHERE word like '"+str+"'"