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

这是我的数据库类:

公共类 DatabaseHandler 扩展 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;
}

}

4

3 回答 3

2

在您的数据库类代码中添加一种方法

如果你想删除 Table All Raw 然后使用

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

其他你想删除表中的特定 Raw

 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

                    }
                });
于 2012-11-05T13:02:26.170 回答
0

在 Helper 类中添加一个方法:

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

使用标签名称调用此方法。您将onItemClick在列表视图中获得方法OnItemClickListener

于 2012-11-05T12:42:37.147 回答
0

如果您想在删除数据后更新列表,则可以使用。

适配器.notifyDataSetChanged(); // 这里的适配器是你的适配器对象。

于 2012-11-06T07:06:38.017 回答