从数据库中删除项目后,列表视图未更新。它在我重新启动应用程序时更新

时间:2017-10-11 06:05:02

标签: java

package com.JohnPuwein.DATABASE; import android.database.sqlite.*; import android.content.*; import android.database.*;

public class DBHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "mylist.db";
    public static final String TABLE_NAME = "mylist_data";
    public static final String COL1 = "ID";
    public static final String COL2 = "ITEM1";

    public DBHelper(Context context)    {
        super(context, DATABASE_NAME, null, 1);
    }
    @Override
    public void onCreate(SQLiteDatabase db)     {
        String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + " ITEM1 TEXT)";
        db.execSQL(createTable);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)    {
        db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
        onCreate(db);
    }
    public boolean addData(String item1)    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL2, item1);

        long result = db.insert(TABLE_NAME, null, contentValues);

        if (result == -1)       {
            return false;
        }       else        {
            return true;
        }
    }
    public Cursor getListContents()     {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
        return data;
    }   public Integer deleteData(String Item1)     {       SQLiteDatabase db = this.getWritableDatabase();         int i = db.delete(TABLE_NAME, "Item1=?", new String[]{Item1});      db.close();         return i;   } }

1 个答案:

答案 0 :(得分:0)

如果在调用delete方法

后立即使用Cursor适配器,则可以调用myCursorAdapter.changeCursor(newCursor);

如果您同时使用游标或自定义适配器,则可以调用此函数myCursorAdapter.notifyDataSetChanged()

希望它可以帮助你