我有问题使用ListView
删除DataBase
。我可以删除ListView
,但当我再次打开Activity
时,我收到错误消息。我有两个类,即UserList.class和MySQLiteHelpre.class。在我的UserList.class中,我将其扩展为onItemLongClick
并添加方法06-03 09:13:43.880 18615-18615/com.sinergi.los.activity E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.sinergi.los.activity, PID: 18615
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sinergi.los.activity/com.sinergi.los.activity.UserList}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2603)
at android.app.ActivityThread.access$900(ActivityThread.java:174)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5752)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74)
at com.sinergi.los.dao.InfoPokokDao.cursorToInfoPokok(InfoPokokDao.java:75)
at com.sinergi.los.dao.InfoPokokDao.getById(InfoPokokDao.java:174)
at com.sinergi.los.dao.PermohonanKreditDao.cursorToFormPK(PermohonanKreditDao.java:56)
at com.sinergi.los.dao.PermohonanKreditDao.getAllFormPK(PermohonanKreditDao.java:90)
at com.sinergi.los.activity.UserList.onCreate(UserList.java:102)
at android.app.Activity.performCreate(Activity.java:5600)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2508)
。
ListView
这是我的listAdapter = new ArrayAdapter<String>(this, R.layout.activity_row_list, userList);
mainListView.setAdapter(listAdapter);
mainListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
final AlertDialog.Builder b = new AlertDialog.Builder(UserList.this);
b.setIcon(android.R.drawable.ic_dialog_alert);
b.setMessage("Ingin menghapus data?");
b.setPositiveButton("Ya",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
IDTable = IDList.get(position);
MySQLiteHelper db=new MySQLiteHelper(getApplicationContext());
db.delete("" + IDTable);
userList.remove(position);
UserList.this.listAdapter.notifyDataSetChanged();
}
});
b.setNegativeButton("Tidak",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
b.show();
return true;
}
});
}
代码
dbHelper
这是public void delete(String id)
{
SQLiteDatabase db = this.getReadableDatabase();
db.delete(TABLE_INFO_POKOK, COLUMN_ID + "=?", new String[]{id});
db.close();
}
{{1}}
答案 0 :(得分:1)
根本原因:
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
您的数据库中的表是空的(因此,光标为空),但您正试图获取其中的内容。在您查询任何内容之前,解决方案始终会检查cursor.moveToFirst()
。
if (cursor != null && cursor.moveToFirst()) {
// cursor.getLong...
}
答案 1 :(得分:0)
这可能会有所帮助。
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.getsavedcontacts();
c.moveToPosition(position);// adapter position
String id = c.getString(c.getColumnIndex(Constants.KEY_ID));
db.deleteRow(Long.parseLong(id));//remove entry from database according to rowID
arr.remove(info.position); //remove entry from arrayadapter, will remove entry from listview
adapter.notifyDataSetChanged();
c.close();