我可以請求幫助...我管理添加數據到數據庫..但我有問題刪除數據..因爲我不能從我的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 ~~希望大家明白我的問題..^_^
感謝您的代碼...我用你的代碼,但我不是複製all..just參加onListItemClick..but當我運行,它顯示error..which說indexoutofbound ...我會編輯和複製結果爲你在我的答案波紋管.. – iWantToLearn 2013-02-09 14:18:10
你得到的錯誤,因爲你想從一個尚未填充的集合中獲取的東西然而。 'idList.add(c.getLong(getIdIndex));'是至關重要的。這是不能省略的。 – mango 2013-02-09 14:32:54
yeaaahhh .. !!! ...它工作.....很多很多很多謝謝你@mango .... – iWantToLearn 2013-02-09 14:45:34