2013-02-08 35 views
1

我可以請求幫助...我管理添加數據到數據庫..但我有問題刪除數據..因爲我不能從我的ListView刪除基於ID .. 。如何獲得ID ?? ..這是我的代碼..在ListActivity中刪除數據庫條目android

這個類的名稱ViewActivity.java在那裏我顯示我的數據庫內容...我只只在我的ListView顯示一列

String query = "select * from " + helper.TABLE_PEKERJA; 

     Cursor c = database.rawQuery(query, null); 

     if(c!=null) 
     { 
      c.moveToFirst(); 
      int getNamaIndex = c.getColumnIndex(helper.COL_NAMA); 

      if(c.isFirst()) 
      { 
       do{ 
        String nama = c.getString(getNamaIndex); 
        result.add(nama); 
       }while(c.moveToNext()); 
      } 
     } 

     this.setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,result)); 
     getListView().setTextFilterEnabled(true); 

在我的DBHelper中,我創建了一個從我的數據庫表中刪除行的方法,這裏是..

public void deleteData(SQLiteDatabase db, String id) 
{ 
    db.delete(TABLE_PEKERJA, COL_ID + " = " +id, null); 
} 

則仍然在班上ViewActivity.java,我在部分實施方法onListItemClick

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 

    DBHelper helper = new DBHelper(this); 
    database = helper.getWritableDatabase();   

    Object o = this.getListAdapter().getItem(position); 
    String keyword = o.toString(); 
    helper.deleteData(database, keyword); 
    Toast.makeText(this, "Data " + keyword + " removed" , Toast.LENGTH_SHORT).show(); 

} 

也得到這個error..this誤差只有我可以查看......這個錯誤

Object o = this.getListAdapter().getItem(position); 

02-09 02:34:23.729: E/Database(28923): at com.example.untukmuna.ViewContentActivity.onListItemClick(ViewContentActivity.java:69) 

所以,我的問題是

1)如何刪除我的數據庫錶行,當我按ListView中的項從ID 2)創建數據庫時,有db.open()...但我應該在哪裏關閉數據庫購買放置db.close()...?

thanksss ~~希望大家明白我的問題..^_^

回答

0

,它通常做的方式是,你只要從列表中單擊參數id

protected void onListItemClick(ListView l, View v, int position, long id) <------ 

但你不能馬上做到這一點,因爲你ListView都插着一個ArrayAdapter,而不是一個CursorAdapter。現在,id將始終爲0或某些無意義的值。如果您需要執行此類任務,我建議您更換適配器。

但是,如果你需要你的適配器,那麼你可以做的是做一個相應的id收集某種你填寫,當你循環你的數據庫填寫result。然後它就像從onListItemClick參數中獲得position那麼簡單,然後從id集合獲得id

當然,您必須確保在刪除列表視圖的行時刪除此集合的值。


嘗試以下操作:

private DBHelper helper; 
private SQLiteDatabase db; 
private ArrayList<String> result = new ArrayList<String>(); 
private ArrayList<Long> idList = new ArrayList<Long>(); 
private ArrayAdapter<String> adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    helper = new DBHelper(this); 
    db = helper.getWritableDatabase();  

    String query = "select * from " + helper.TABLE_PEKERJA; 
    Cursor c = db.rawQuery(query, null); 
    int getIdIndex = c.getColumnIndex(helper.COL_ID); 
    int getNamaIndex = c.getColumnIndex(helper.COL_NAMA); 

    while (c.moveToNext()) { 
     idList.add(c.getLong(getIdIndex)); 
     result.add(c.getString(getNamaIndex)); 
    } 

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, result); 
    this.setListAdapter(adapter); 
    getListView().setTextFilterEnabled(true); 
} 

public boolean deleteData(Long id) { 
    return db.delete(TABLE_PEKERJA, COL_ID + " = " + id, null) > 0; 
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    Long idValue = idList.get(position); 
    boolean isDeleted = deleteData(idValue); 
    Toast.makeText(this, "boolean value for database deletion: " + String.valueOf(isDeleted), Toast.LENGTH_SHORT).show(); 

    idList.remove(position); 
    result.remove(position); 
    adapter.notifyDataSetChanged(); //refresh so that we can see listview changes 
} 
+0

感謝您的代碼...我用你的代碼,但我不是複製all..just參加onListItemClick..but當我運行,它顯示error..which說indexoutofbound ...我會編輯和複製結果爲你在我的答案波紋管.. – iWantToLearn 2013-02-09 14:18:10

+0

你得到的錯誤,因爲你想從一個尚未填充的集合中獲取的東西然而。 'idList.add(c.getLong(getIdIndex));'是至關重要的。這是不能省略的。 – mango 2013-02-09 14:32:54

+0

yeaaahhh .. !!! ...它工作.....很多很多很多謝謝你@mango .... – iWantToLearn 2013-02-09 14:45:34

相關問題