简体   繁体   English

如何在RecyclerView适配器中更新游标

[英]How to update the Cursor in a RecyclerView Adapter

i built a RecyclerView Adapter which reads data out of a SQLite database. 我建立了一个RecyclerView适配器,可以从SQLite数据库中读取数据。 I have trouble updating the Adapter after the database were edited. 编辑数据库后,更新适配器时遇到问题。

I know there are a few examples which show how to implement a swapCursor method into a RecyclerView Adapter, but they all contain a lot of additional code which i dont really know how to fit in. I feel like there is too little information about this out there. 我知道有一些示例显示了如何在RecyclerView Adapter中实现swapCursor方法,但是它们都包含很多我不知道如何适合的其他代码。我觉得关于这方面的信息太少了那里。

In the following method i delete a database row depending on its position in the Adapter. 在以下方法中,我将根据数据库行在适配器中的位置删除该行。 When i call adapter.notifyItemRemoved(position) afterwards, it re-adds the item at the bottom. 之后,当我调用adapter.notifyItemRemoved(position)时,它将在底部重新添加该项目。 When i close and re-open the Activity, the database is correct. 当我关闭并重新打开活动时,数据库是正确的。 The problem is, that i dont swap the Cursor and i dont know how. 问题是,我不交换游标,也不知道怎么做。

@Override
public void onDeleteClick(int position) {
    SQLiteDatabase database = new ExampleSQLiteHelper(this).getWritableDatabase();
    Cursor cursor = database.rawQuery("select * from " + ExampleContract.ExampleEntry.TABLE_NAME, null);
    cursor.moveToPosition(position);
    database.delete(ExampleContract.ExampleEntry.TABLE_NAME, ExampleContract.ExampleEntry._ID + "=?", new String[] {cursor.getString(cursor.getColumnIndexOrThrow(ExampleContract.ExampleEntry._ID))});
    //Here i have to swap the Cursor and notify the Adapter
}
  1. How do i swap the Cursor properly and close the old one? 我如何正确地调换光标并关闭旧的光标?
  2. What Cursor do i have to provide? 我必须提供什么游标? The one that queries the database for deletion is not up-to-date. 用于查询数据库以进行删除的数据库不是最新的。 So i would have to create a 2nd Cursor after deleting the row. 所以我将不得不删除行后创建第二个光标。 That seems like a lot of Cursor creating and closing to me. 似乎很多Cursor正在创建和关闭。
  3. Is it ok to delete a row depending on its position in the Adapter, like i do in the above example? 是否可以像在上面的示例中一样根据行在适配器中的位置删除行? Since my ViewHolder Class is static, i cant access the Adapter's Cursor in there and can only get the Adapter position into the onClick method. 由于我的ViewHolder类是静态的,因此我无法在其中访问适配器的光标,并且只能将适配器位置放入onClick方法中。

Edit: 编辑:

Here is my approach for change the Cursor. 这是我更改光标的方法。 It does work, but i dont know if its correct. 它确实有效,但是我不知道它是否正确。 Can anyone confirm this and are the Cursors closed prperly? 谁能确认这一点,并且游标是否已正确关闭?

@Override
public void onDeleteClick(int position) {
    SQLiteDatabase database = new ExampleSQLiteHelper(this).getWritableDatabase();
    Cursor cursor = database.rawQuery("select * from " + ExampleContract.ExampleEntry.TABLE_NAME, null);
    cursor.moveToPosition(position);
    database.delete(ExampleContract.ExampleEntry.TABLE_NAME, ExampleContract.ExampleEntry._ID + "=?", new String[] {cursor.getString(cursor.getColumnIndexOrThrow(ExampleContract.ExampleEntry._ID))});
    cursor.close();

    Cursor newCursor = database.rawQuery("select * from " + ExampleContract.ExampleEntry.TABLE_NAME, null);
    mAdapter.changeCursor(newCursor);
    mAdapter.notifyItemRemoved(position);
}

Adapter: 适配器:

public void changeCursor(Cursor newCursor) {
    Cursor oldCursor = mCursor;
    mCursor = newCursor;
    oldCursor.close();
}

That seems like a lot of Cursor creating and closing to me. 似乎很多Cursor正在创建和关闭。

Well, without storing any extra data associated with the item you're deleting in the ViewHolder or adapter, you will need to perform a selection and extract the column. 好吧,无需在ViewHolder或适配器中存储与要删除的项目相关的任何额外数据,您将需要执行选择并提取列。 I would like to think adding a WHERE condition would benefit you, but moving to the necessary position is fine, in my opinion. 我想认为添加WHERE条件将使您受益,但我认为移至必要位置可以。

Since my ViewHolder Class is static, i cant access the Adapter's Cursor in there 由于我的ViewHolder类是静态的,因此我无法在其中访问适配器的游标

I don't think you want the adapter cursor since it's already iterating the rows, but if your click method is defined onto the adapter class, you can get the cursor there. 我认为您不需要适配器游标,因为它已经在对行进行迭代,但是如果将click方法定义在适配器类上,则可以在那里获得游标。 Otherwise adapter.this.cursor might work. 否则, adapter.this.cursor可能起作用。

Regarding swapping the cursor, most examples you dismissed have that logic. 关于交换光标,您忽略的大多数示例都具有这种逻辑。 Checkout , Using the recyclerview with a database 结帐, 将recyclerview与数据库一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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