删除列表视图上的项目但不会从SQLite中删除

时间:2012-12-23 03:09:03

标签: java android sqlite listview

我使用此方法删除SQLite数据库中的Item:

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

    db.beginTransaction();
    db.delete(TABLE_NAME, ITEMS_COLUMN + " =?",  new String[] {item});
    db.setTransactionSuccessful();
    db.endTransaction();
    db.close();
}

这是我的ListView:

String nameString = (arg0.getItemAtPosition(arg2)).toString();
Log.d("itemtodelete", nameString);
db.deleteItem(nameString);
magicAdapter.remove(nameString);
magicAdapter.notifyDataSetChanged();

问题在于,当我删除listview上的项目时,该项目消失但当我重新打开它时,该项目仍然存在,因为这不会从数据库中删除。 我将尝试用图像解释这个:

I add an item

Here it is

I delete this

Bye bye Item

Re-open the list

Here it is again!

3 个答案:

答案 0 :(得分:1)

这意味着从db中删除存在一些问题。只需用

替换deleteItem()中的第二行
int x = db.delete(TABLE_NAME, ITEMS_COLUMN + " =?",  new String[] {item}
Log.d("deletedItem", x);

此处x将是已删除的行数。删除后检查x的值,如果删除成功,则应大于0。如果不是那么这意味着查询是错误的,我们需要数据库模式来纠正它。从您的ListView实施代码中可以清楚地看出您的nameString本身是错误的。您将在arraylist中添加整个Item并传递给适配器。当您在onItemClick对话框中获取项目时,您正在使用此代码

 String nameString = (arg0
                                            .getItemAtPosition(arg2))
                                            .toString();

这里arg0.getItemAtPosition(arg2)会返回一个Item对象。你必须做这样的事情。

Item tempItem=(Item)items.get(arg2);
String nameString=tempItem.getName();

其中getName()将返回项目的名称。

答案 1 :(得分:0)

我所做的改变:

public void onClick(DialogInterface dialog, int which) {
    String nameString = (arg0.getItemAtPosition(arg2)).toString();

    Log.d("itemtodelete", nameString);

    db.deleteItem(nameString);

    magicAdapter.remove(nameString);
    magicAdapter.notifyDataSetChanged();

public void onClick(DialogInterface dialog, int which) {
    String nameString = (arg0.getItemAtPosition(arg2)).toString();
    String nameStringData = nameString.substring(6,
    nameString.indexOf("Priority Level:") - 1);
    Log.d("itemtodelete", nameStringData);
    db.deleteItem(nameStringData);
    magicAdapter.remove(nameString);

    magicAdapter.notifyDataSetChanged();

如果您有更好的建议,请发表回答。

答案 2 :(得分:-1)

是的,这就是问题所在。

在logCat上

deletedItem = 0,以便我的数据库:

public class DatabaseHolder extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "ItemsList";
    private static final String TABLE_NAME = "Items";
    private static final String ITEMS_COLUMN = "items_name";
    private static final String PRIORITY_COLUMN = "Priority";
    private static final String ID_COLUMN = "Items";

    private static int DATABASE_VERSION = 1;

    private static String QUERY = "CREATE TABLE " + TABLE_NAME + "("
            + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ITEMS_COLUMN
            + " TEXT NOT NULL, " + PRIORITY_COLUMN + " TEXT NOT NULL);";

    public DatabaseHolder(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(QUERY);

    }

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

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        this.onCreate(db);

    }

    // Sharer!

    public void addItem(String item_name, String priority) {

        if (!duplicate(item_name)) {
            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put(ITEMS_COLUMN, item_name);
            values.put(PRIORITY_COLUMN, priority);

            db.beginTransaction();
            db.insert(TABLE_NAME, null, values);
            db.setTransactionSuccessful();
            db.endTransaction();
            db.close();
        }
    }

    public boolean duplicate(String name) {

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor c = db.query(TABLE_NAME, null, ITEMS_COLUMN + " =?",
                new String[] { name }, null, null, null);
        int temp = c.getCount();
        c.close();
        db.close();
        if (temp > 0)
            return true;
        else
            return false;
    }

    public ArrayList<Item> getAllItems() {

        SQLiteDatabase db = this.getWritableDatabase();

        ArrayList<Item> item = new ArrayList<Item>();

        db.beginTransaction();

        Cursor cursor = db.query(TABLE_NAME, new String[] { this.ITEMS_COLUMN,
                this.PRIORITY_COLUMN }, null, null, null, null, PRIORITY_COLUMN
                + " DESC");

        while (cursor.moveToNext()) {

            Item itemTemp = new Item(cursor.getString(cursor
                    .getColumnIndexOrThrow(ITEMS_COLUMN)), new Level(
                    Integer.parseInt(cursor.getString(cursor
                            .getColumnIndexOrThrow(PRIORITY_COLUMN)))));
            item.add(itemTemp);

        }

        cursor.close();
        db.endTransaction();
        db.close();
        return item;

    }

    public void deleteItem(String item){

        SQLiteDatabase db = this.getWritableDatabase();
        int x = db.delete(TABLE_NAME, ITEMS_COLUMN + " =?",  new String[] {item});
        Log.d("deletedItem", String.valueOf(x) );
        db.beginTransaction();

        db.delete(TABLE_NAME, ITEMS_COLUMN + " =?",  new String[] {item});

        db.setTransactionSuccessful();

        db.endTransaction();

        db.close();


    }

}