2012-07-07 61 views
1

我想從上下文菜單中選擇一個刪除菜單項時刪除一個sqlite數據庫記錄。 我創建了一個上下文菜單,它正在工作,但我無法刪除數據庫行。有什麼辦法刪除記錄?我該怎麼辦?如何刪除上下文菜單上的選定行?

public void deleteItem(dbItem item) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    db.delete(TABLE_ITEMS, items_id + " = ?", 
     new String[] { String.valueOf(item.getID()) }); 
    db.close(); 
} 

public boolean onContextItemSelected(MenuItem item) { 
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); 

    int menuitemindex = item.getItemId(); 


    if(item.getItemId()==0) 
    { 
     //code for send text 
    } 
    else if(item.getItemId()==1) // Delete menu item 
    { 
     // db.deleteItem(item2); 
    } 
    return super.onContextItemSelected(item);  
} 

我如何使用我的deleteItem()上下文菜單部分程序?

回答

0

我發現了12天后後的解決方案:)

public boolean onContextItemSelected(MenuItem item) { 
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); 

    int menuitemindex = item.getItemId(); 


    if(menuitemindex == 0) 
    { 
     //code for send text 
    } 
    else if(menuitemindex == 1) 
    {   
     dbItem silinen = adapter.getItem(info.position);  
     adapter.remove(silinen);    
     adapter.notifyDataSetChanged(); 
     db.deleteItem(silinen, getApplicationContext());   
    } 
    return super.onContextItemSelected(item);  
} 

和刪除程序:

public void deleteItem(dbItem item, Context context) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    try { 

     db.delete(TABLE_ITEMS, items_id + " = ?", 
       new String[] { String.valueOf(item.getID())}); 

    } catch (Exception e) 
    { 
     Log.e("insert",e.getMessage()); 
    } 


    Toast.makeText(context, "Item succesfully deleted. Name: "+ item.getName(), Toast.LENGTH_LONG).show(); 

    db.close();   

} 
+0

哪裏'adapter'從何而來? – 2015-06-17 09:50:10

相關問題