2015-06-03 61 views
0

我有问题删除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(); 
} 
+0

不,只是按行选择 – Wisnu

回答

1

根源在这里删除代码:

Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 

你的数据库表是空的(一结果,光标是空的),但是你试图在其中获得某些东西。在查询任何内容之前,解决方案总是检查cursor.moveToFirst()

if (cursor != null && cursor.moveToFirst()) { 
    // cursor.getLong... 
} 
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(); 
相关问题