我在SQLite数据库中有一些数据。我有一个内容提供程序,它将从数据库中获取数据。现在的问题是我如何实现cursorLoader与recyclerview一起工作?
还有,谁能解释一下为什么我们需要将数据从光标转移到一个对象,以显示在一个列表视图/回收视图中,而不是直接从光标?
例如,在自定义cursorAdapter类中,
Person person = new Person(cursor.getString(cursor.getColumnIndexOrThrow(PERSON_NAME)));
textview.setText = person.getName();
字符串
或
textview.setText = cursor.getString(cursor.getColumnIndexOrThrow(PERSON_NAME));
型
以上哪一种方法比较好?
在过去,我们曾经有listview和gridview,现在它们似乎合并成了recyclerview。那么,我如何实现一个基于网格的recyclerview?
4条答案
按热度按时间l3zydbqr1#
一般来说,您应该尝试分离视图和数据职责。因此,您需要提前从数据库中获取所有对象,然后设置一个Adapter,如下所示:
字符串
然后,您可以通过以下方式将适配器绑定到RecyclerView:
型
为了回答你的第二个问题(“在过去,我们曾经有列表视图和网格视图,现在似乎它们合并成了回收视图。那么,我如何实现一个基于网格的recyclerview?“):
将LayoutManager绑定到RecyclerView时,您可以决定使用哪一个:
型
或者是
型
有几个LayoutManager。了解更多here .
UPDATE:您不必提前加载所有项,只需将setItems重命名为addItems,就可以开始了
1wnzp6jl2#
有几个Github gist/项目,如this和this,展示了这样的用例。
虽然您将使用一个为游标适配器定制的Adapter,但您将像往常一样使用GridLayoutManager/LinearLayoutManager。
9gm1akwq3#
我觉得你可以直接使用Custom**
CursorAdapter
为RecyclerView
,这样就不用把Cursor转换成ArrayList**了:字符串
希望它对你有用。:)
jvlzgdj94#
使用RecyclerView显示使用CursorLoader从ContentProvider获取的联系人列表。
1. ShowContactBook类其中显示回收器视图
字符串
}
**1. CustomCursorAdapter类:**CustomCursorAdapter类是RecyclerView.Adapter的自定义实现,用于在RecyclerView中显示Cursor中的数据。在所提供的代码的上下文中,此适配器用于显示从ContentProvider获取的联系人列表。
型
}