我有問題刪除DataBase
使用ListView
。我可以刪除DataBase
,但是當我再次打開ListView
時,出現錯誤。我有兩個類是UserList.class和MySQLiteHelpre.class。在我的UserList.class中,我將它擴展爲Activity
並添加方法onItemLongClick
。在Android中刪除數據庫和ListView
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();
}
不,只是按行選擇 – Wisnu