2012-06-12 166 views
0

我需要更改方法onListItemClick()删除列表上的项目..想法? 我不知道如何适应我的课程。我有一个Db班和一个助手班。从列表视图中删除项目

下面是代码:

Cursor cursor = db.rawQuery("select * from " + PRODUCT_TABLE, null); 

     setListAdapter(new CursorAdapter(this, cursor, true) { 

     @Override 
     public View newView(Context context, Cursor cursor, ViewGroup parent){ 

      TextView textView = new TextView(ProdottiSelectionActivity.this); 
      return textView; 
     } 

     @Override 
     public void bindView (View view, Context context, Cursor cursor){ 

      TextView textView = (TextView) view; 
      textView.append(cursor.getString(1)); 
      textView.append("\n"); 
      textView.append(cursor.getString(2)); 
      textView.append("\n"); 
      textView.append(cursor.getString(3)); 
      textView.append("\n"); 
      textView.append(cursor.getString(4)); 
      } 
     }); 
    } 

@Override 
protected void onDestroy(){ 
    super.onDestroy(); 
    dbHelper.close(); 
    } 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    setResult(1, new Intent(String.valueOf(id))); 
    finish(); 
} 

回答

1

你需要像下面

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    db.open(); 
    //you have to determine user clicked on which item 
    String[] whereArgs={String.valueOf(item_id)}; 
    db.delete(DATABASE_TABLE_4,"item_id = ?",whereArgs);  
    cursor.requery(); //deprecated 
    adapter.notifyDataSetChanged(); //deprecated 
    db.close(); 
} 
相关问题