[英]How to change the order of a LiveData List?
I'm trying to change the order in which the wrestlers are displayed in my RecyclerView.我正在尝试更改摔跤手在我的 RecyclerView 中的显示顺序。 I want to give the user the option displaying the list by first name in Ascending, Descending, and Random order.我想为用户提供按升序、降序和随机顺序按名字显示列表的选项。 How can I change the order of the items in the LiveData list?如何更改 LiveData 列表中项目的顺序?
The list of Wrestlers is stored in a Room Database, I query the database for all the wrestlers in ascending order by default.摔跤手列表存储在房间数据库中,默认情况下,我按升序查询所有摔跤手的数据库。
WrestlersDao摔跤手道
@Query("SELECT * FROM wrestler_table WHERE ORDER BY mFirstName :sortOrder
LIMIT :size")
LiveData<List<WrestlersEntity>> getAllWrestlers(int size, String sortOrder);
MainActivityViewModel MainActivityViewModel
private LiveData<List<WrestlersEntity>> mWrestlersList;
The mWrestlersList is displayed in my RecyclerView. mWrestlersList 显示在我的 RecyclerView 中。
MainActivity主要活动
mMainActivityViewModel.getWrestlersList().observe(this, wrestlersEntities -> {
adapter.submitList(wrestlersEntities);
});
I was thinking I could Query the Database again, would I have to create a new query in the DAO to change the order of the LiveData List, I'm not sure if there is a way to change the ASC programmatically in the original query.我在想我可以再次查询数据库,我是否必须在 DAO 中创建一个新查询来更改 LiveData 列表的顺序,我不确定是否有办法在原始查询中以编程方式更改 ASC。
The other option that sounds like it might work is to use a Transformation.map, but I'm struggling to understand this concept.听起来可能可行的另一个选项是使用 Transformation.map,但我很难理解这个概念。
MainActivityViewModel MainActivityViewModel
Transformations.map(mWrestlersList) {
Collections.shuffle((List<?>) mWrestlersList);
}
Any help here would be greatly appreciated.在这里的任何帮助将不胜感激。
Pass the column-name and sort-order as parameters;将列名和排序顺序作为参数传递; eg.例如。 for sorted pagination:对于排序分页:
// getAllWrestlers("mFirstName", "ASC", 50, 0);
@Query("SELECT * FROM wrestler_table ORDER BY :columnName :sortOrder LIMIT :limit OFFSET :offset")
LiveData<List<WrestlersEntity>> getAllWrestlers(String columnName, String sortOrder, int limit, int offset);
Because unless passing them as parameters, one cannot really control the result-window.因为除非将它们作为参数传递,否则无法真正控制结果窗口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.