如何从GridView项目中删除SQLite数据?

问题描述 投票:0回答:2

我不能为我的生活弄清楚如何通过单击该单元格来仅删除填充到单个gridView单元格中的SQLite中的数据。任何帮助将不胜感激。我已经标记了我要从SQLite中插入要删除的代码的位置。这是我的列表代码:

public class FishList extends AppCompatActivity {

GridView gridView;
ArrayList<Fish> list;
FishListAdapter adapter;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fish_list_activity);

    gridView =  findViewById(R.id.gridView);
    list = new ArrayList<>();
    adapter = new FishListAdapter(this, R.layout.fish_items, list);
    gridView.setAdapter(adapter);

    Cursor cursor = sqLiteHelper.getData("SELECT * FROM FISH");
    list.clear();
    while (cursor.moveToNext()) {

        String species = cursor.getString(cursor.getColumnIndex("species"));
        String date = cursor.getString(cursor.getColumnIndex("date"));
        String weight = cursor.getString(cursor.getColumnIndex("weight"));
        String length = cursor.getString(cursor.getColumnIndex("length"));
        String sex = cursor.getString(cursor.getColumnIndex("sex"));
        String bait = cursor.getString(cursor.getColumnIndex("bait"));
        String method = cursor.getString(cursor.getColumnIndex("method"));
        byte[] image = cursor.getBlob(cursor.getColumnIndex("image"));

        list.add(new Fish(species, date, weight, length, sex, bait, method, image));
    }
    adapter.notifyDataSetChanged();
}
public void returndash(View view){
    Intent myintent = new Intent(FishList.this, Dash.class);
    startActivity(myintent);
}
public void returnNew(View view){
    Intent myintent = new Intent(FishList.this, NewCatch.class);
    startActivity(myintent);
}
public void deleteEntry(View view){

    AlertDialog.Builder builder1 = new AlertDialog.Builder(FishList.this);
    builder1.setMessage("Are you sure you want to delete this Catch?");
    builder1.setCancelable(true);
    builder1.setPositiveButton(
            "Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    ////////////
                    //Delete database strings and byte[] image here
                    ////////////
                }
            });
    builder1.setNegativeButton(
            "No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert11 = builder1.create();
    alert11.show();
}
}

这是我的SQLiteHelper:

public class SQLiteHelper extends SQLiteOpenHelper {

public SQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);


}
public void queryData(String sql) {
    SQLiteDatabase database = getWritableDatabase();
    database.execSQL(sql);
}
public void insertData(String species, String date, String weight, String length, String sex,String bait,String method, byte[] image) {
    SQLiteDatabase database = getWritableDatabase();
    String sql = "INSERT INTO FISH VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

    SQLiteStatement statement = database.compileStatement(sql);
    statement.clearBindings();

    statement.bindString(1, species);
    statement.bindString(2, date);
    statement.bindString(3, weight);
    statement.bindString(4, length);
    statement.bindString(5, sex);
    statement.bindString(6, bait);
    statement.bindString(7, method);
    statement.bindBlob(8, image);

    statement.executeInsert();
}
public void deleteDatabase(){
    SQLiteDatabase database = sqLiteHelper.getWritableDatabase();
    database.delete("FISH", "1", new String[0] );
    database.close();
}
public Cursor getData(String sql) {
    SQLiteDatabase database = getReadableDatabase();
    return database.rawQuery(sql, null);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}

也许其他人可以对此有所了解。 我尝试了很多不同的方法。 我所能做到的就是擦除整个数据库。

android sqlite gridview android-sqlite
2个回答
1
投票

你可以使用: -

public void deleteCatch(String species, String date, String weight, String length, String sex, String bait){
    SQLiteDatabase database = sqLiteHelper.getWritableDatabase();
    database.delete("FISH",
        "species=? AND " +
        "date=? AND " +
        "weight=? AND " +
        "length=? AND " +
        "sex=? AND " +
        "bait=?",
        new String[]{species,date,weight,length,sex,bait});
    );
    database.close();
}

这确实假设列足以唯一地标识行(可以根据需要使用更少/更多)。这将通过传递从列表中的相应Fish提取的相应值来调用。

替代更有效的方法

但是,SQLite会自动为每一行提供unqiue标识符(rowid)(除非使用WITHOUT ROWID定义表)。这个rowid通常是隐藏的。通常一列将被定义为?? INTEGER PRIMARY KEY其中??是列名。这是专门处理的,因为列名称成为rowid的别名,然后不被隐藏。使用这样的别名是非常常见的并且使用起来也非常有效(上述方法几乎不那么有效)。

注意?? INTEGER PRIMARY KEY AUTOINCREMENT也会创建别名。然而,它的使用,很少需要,导致开销。通常AUTOINCREMENT最好不要使用。

因此,如果您已包含此类列,请假设您将其命名为_id以用于说明目的。

如果您将Fish类修改为具有名为id的成员,则在创建列表数组时添加以下内容: -

    String id = Long.toString(cursor.getlong(cursor.getColumnIndex("_id")));

并修改将鱼添加到列表中: -

    ......
    list.add(new Fish(id, species, date, weight, length, sex, bait, method, image)); // Note id added

然后,您就可以从Fish实例访问_id值。

No rowid alias!

如果您没有定义这样的列,则可以在查询数据库以获取游标时从“隐藏”rowid生成合适的列。例如改变: -

Cursor cursor = sqLiteHelper.getData("SELECT * FROM FISH");

Cursor cursor = sqLiteHelper.getData("SELECT rowid AS _id, * FROM FISH");

然后上面的更改将适用于以前的更改。

更简单的deleteCatch方法

然后你可以有一个更简单的deleteCatch方法: -

public void deleteCatch(long id){
    SQLiteDatabase database = sqLiteHelper.getWritableDatabase();
    database.delete("FISH",
        "_id=?",
        new String[]{Long.toString(id)});
    );
    database.close();
}

您只需要从列表中的相应Fish中提取一个值。


注意!您可能会遇到在数据库中存储图像的问题(例如,A CursorWindow限制为2MB)。许多人建议将图像存储在数据库之外,并将文件的路径存储在数据库中。


1
投票

在您的Fish POJO中,跟踪行ID。当我(以前)以这种方式创建Android数据库时,我会调用INTEGER PRIMARY AUTOINCREMENT行的ID。

考虑到这一点,我将它作为int变量存储在我的模型类中(在您的情况下,Fish),然后当我想对它执行操作(例如编辑/删除)时,我会参考在SQL语句中使用其中id =?的行ID。

© www.soinside.com 2019 - 2024. All rights reserved.