2012-11-05 84 views
0

當我在列表視圖中單擊一個項目時,如何刪除數據庫中的數據?請給我一些代碼。 這是我的方法來選擇項目的列表視圖:刪除數據表格數據庫?

listview.setOnItemClickListener(new OnItemClickListener(){ 
     public void onItemClick(AdapterView adapterView, View convertView, int position, long id) 
     { 
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(AndroidSpinnerFromSQLiteActivity.this); 


      alertDialog.setTitle("Confirm Delete..."); 


      alertDialog.setMessage("Are you sure you want delete this?"); 


      alertDialog.setIcon(R.drawable.delete); 


      alertDialog.setPositiveButton("YES", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog,int which) { 


         } 
        }); 

      alertDialog.setNegativeButton("NO", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 

         } 
        }); 

      alertDialog.show(); 
      } 
     }); 
    }; 

這是我的數據庫類:

公共類數據庫處理器擴展SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1; 


private static final String DATABASE_NAME = "spinnerExample"; 


private static final String TABLE_LABELS = "labels"; 


private static final String KEY_ID = "id"; 
private static final String KEY_NAME = "name"; 
public DatabaseHandler(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 


@Override 
public void onCreate(SQLiteDatabase db) { 

    String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "(" 
      + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT" +")"; 
    db.execSQL(CREATE_CATEGORIES_TABLE); 
} 


@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS); 


    onCreate(db); 
} 


public void insertLabel(String label){ 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_NAME, label); 


    db.insert(TABLE_LABELS, null, values); 
    db.close(); 
} 


public List<String> getAllLabels(){ 
    List<String> labels = new ArrayList<String>(); 


    String selectQuery = "SELECT * FROM " + TABLE_LABELS; 

    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 


    if (cursor.moveToFirst()) { 
     do { 
      labels.add(cursor.getString(1)); 
     } while (cursor.moveToNext()); 
    } 


    cursor.close(); 
    db.close(); 


    return labels; 
} 

}

+0

你是通過一個'ContentProvider'實現的嗎? – Estel

回答

2

在烏拉圭回合的數據庫類代碼添加一個方法

如果u想刪除表中的所有原始然後用

db.execSQL("DELETE from "+Table_Name); 

其他ü要在表中刪除特定原始

db.delete(Table_Name, "Raw_Field_Name" + "='" + Raw_field_value+"'", null); 

列表查看項目點擊事件使用

Listview lv; 
//on create method 

    lv = (ListView) findViewById(R.id.list_contact_name); 

       lv.setOnItemClickListener(new OnItemClickListener() 
       { 

        public void onItemClick(AdapterView<?> arg0, View v, int position, 
          long id) { 

         // call DELETE method here 

        } 
       }); 
+0

我在menthod中需要一些代碼在listview中選擇項目嗎? – TranGia

+0

但我可以使用什麼刪除方法?我試圖找到它。 – TranGia

+0

public boolean deleteData(String Raw_field_value) { return db.delete(Table_Name,「Raw_Field_Name」+「='」+ Raw_field_value +「'」,null); }這個方法聲明在你的數據庫類中,然後使用這個 –

0

在助手類中添加一個方法:

public boolean deleteData(String name) 
{ 
    SQLiteDatabase db=getWritableDatabase(); 
    return db.delete(TABLE_NAME, KEY_NAME+"= ?", new String[]{name})>0; 
} 

用標籤名稱調用此方法。您將在列表視圖OnItemClickListener中獲得onItemClick方法。

+0

我需要一些代碼在我的menthod中選擇項目在列表視圖? – TranGia

0

如果您想在刪除數據後更新列表,則可以使用。

adapter.notifyDataSetChanged(); //適配器是您的適配器對象。