程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> listview-如何刪除數據庫中的數據?

listview-如何刪除數據庫中的數據?

編輯:編程綜合問答
如何刪除數據庫中的數據?

當我點擊列表視圖中的一個選項時,如何刪除數據庫中的數據?我使用下面的方法來選擇列表視圖的選項。

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();
            }
        });
    };

數據庫類:

public class DatabaseHandler extends 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;
}
}

最佳回答:


在你的數據庫類的代碼中添加一個方法。如果你想刪除所有的原始表單,使用

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
                    }
                });
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved