简体   繁体   中英

Delete an Item on Listview but this doesnt deleted from SQLite

I use this method to delete an Item on my SQLite database:

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

And this to my ListView:

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

The problem is that when i delete an item on my listview the item disappears but when I re-open it the item is still there, because this doesn't remove from the database. I 'll try to explain this with images :

我添加一个项目

这里是

我删除了

再见项目

重新打开清单

又来了!

This means that there is some problem with the deleting from the db. Just replace 2nd line in your deleteItem() with

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

Here x would be the number of rows deleted. Check the value of x after deleting, it should be greater than 0 if the deletion was successful. If it is not then that means the query is wrong and we would need the database schema for correcting it. From your ListView implementation code, its clear that your nameString itself is wrong. You are adding the whole Item in the arraylist and passing to the adapter. And when you fetch the item in the onItemClick dialog, you are using this code

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

Here arg0.getItemAtPosition(arg2) would return an Item object. You will have to do something like this.

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

where getName() would return the name of the item.

The change that I did:

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

to

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

If you have a better suggestion please post an answer.

Yes this is the problem.

deletedItem = 0 on logCat so that's my database:

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


    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM