简体   繁体   English

如何更改 LiveData 列表的顺序?

[英]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.因为除非将它们作为参数传递,否则无法真正控制结果窗口。

如何添加和删除<object>从(和到)LiveData <list<object> &gt;?<div id="text_translate"><p> 我有一个用 LiveData&lt;List&gt; currentList 填充的 recyclerView。 我采用 Single&lt;List&gt; 并在 onSuccess 方法中使用 currentList.setValue(people) 添加人员(请参见下面的代码)。 但是当我打开应用程序时,recyclerView 中只有 1 个 object。 我的任务是: 1)当应用程序打开时,它必须将 4 人添加到 recyclerView(即 currentList); 2) 在 onSwipe 方法之后,它必须删除第一个并将 RoomDatabase 中的 1 个新随机添加到 currentList 的末尾。</p><p> 这意味着我需要将对象删除和添加到 LiveData&lt;List&gt; 的方法。 我怎样才能做到这一点? (ViewModel 代码如下)</p><pre> public class PersonViewModel extends AndroidViewModel { private PersonRepository mRepository; private CompositeDisposable composite = new CompositeDisposable(); private Single&lt;List&lt;Person&gt;&gt; mGuyWho; // это нада??? LOBNYA private Single&lt;Person&gt; mGuy; // PSKOV private MutableLiveData&lt;List&lt;Person&gt;&gt; currentList = new MutableLiveData&lt;&gt;(); public PersonViewModel(@NonNull Application application) { super(application); mRepository = new PersonRepository(application); mGuyWho = mRepository.getGuyWho(); //это нада?? LOBNYA mGuy = mRepository.getGuy(); // PSKOV mGuyWho.subscribeOn(Schedulers.io()) // LOBNYA nachalo.observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleObserver&lt;List&lt;Person&gt;&gt;() { @Override public void onSubscribe(Disposable d) { Log.d(TAG, "onSubscribe(Disposable d): called"); composite.add(d); } @Override public void onSuccess(List&lt;Person&gt; people) { Log.d(TAG, "onSuccess: called"); currentList.setValue(people); // here I pass List to LiveData&lt;List&gt; } @Override public void onError(Throwable e) { Log.d(TAG, "onError: called"); Toast.makeText(application, "NO DATA", Toast.LENGTH_SHORT).show(); } }); // LOBNYA konets mGuy.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleObserver&lt;Person&gt;() { @Override public void onSubscribe(Disposable d) { Log.d(TAG, "onSubscribe(Disposable d): called"); composite.add(d); } @Override public void onSuccess(Person person) { Log.d(TAG, "onSuccess: called"); currentList.setValue(person); // there is an error, for I cannot bypass &lt;Person&gt; to LiveData&lt;List&lt;&gt;&gt; } @Override public void onError(Throwable e) { Log.d(TAG, "onError: called"); } }) } LiveData&lt;List&lt;Person&gt;&gt; getCurrentList() { return currentList; } };</pre> </div></list<object></object> - how to add and delete <Object> from (and to) LiveData<List<Object>>?

暂无
暂无

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

相关问题 如何访问“ LiveData”列表中的元素“ Livedata <List<Model> &gt;在科特林? - How to access the elements of the LiveData list "Livedata<List<Model>> in Kotlin? 如何转换 LiveData <List<User> &gt; 到 LiveData <List<String> &gt; 在 ViewModel 中? - How to transform LiveData<List<User>> to LiveData<List<String>> in ViewModel? 带有 Room 和 LiveData 的 MVVM - 如何从 LiveData 获取列表 - MVVM with Room and LiveData - How to get List from LiveData 如何使用 JPA 和 @OrderColumn 更改有序列表的顺序? - How to change the order of an ordered list with JPA and @OrderColumn? 随机化 LiveData <list<names></list<names> - Randomize LiveData<List<names> 如何添加和删除<object>从(和到)LiveData <list<object> &gt;?<div id="text_translate"><p> 我有一个用 LiveData&lt;List&gt; currentList 填充的 recyclerView。 我采用 Single&lt;List&gt; 并在 onSuccess 方法中使用 currentList.setValue(people) 添加人员(请参见下面的代码)。 但是当我打开应用程序时,recyclerView 中只有 1 个 object。 我的任务是: 1)当应用程序打开时,它必须将 4 人添加到 recyclerView(即 currentList); 2) 在 onSwipe 方法之后,它必须删除第一个并将 RoomDatabase 中的 1 个新随机添加到 currentList 的末尾。</p><p> 这意味着我需要将对象删除和添加到 LiveData&lt;List&gt; 的方法。 我怎样才能做到这一点? (ViewModel 代码如下)</p><pre> public class PersonViewModel extends AndroidViewModel { private PersonRepository mRepository; private CompositeDisposable composite = new CompositeDisposable(); private Single&lt;List&lt;Person&gt;&gt; mGuyWho; // это нада??? LOBNYA private Single&lt;Person&gt; mGuy; // PSKOV private MutableLiveData&lt;List&lt;Person&gt;&gt; currentList = new MutableLiveData&lt;&gt;(); public PersonViewModel(@NonNull Application application) { super(application); mRepository = new PersonRepository(application); mGuyWho = mRepository.getGuyWho(); //это нада?? LOBNYA mGuy = mRepository.getGuy(); // PSKOV mGuyWho.subscribeOn(Schedulers.io()) // LOBNYA nachalo.observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleObserver&lt;List&lt;Person&gt;&gt;() { @Override public void onSubscribe(Disposable d) { Log.d(TAG, "onSubscribe(Disposable d): called"); composite.add(d); } @Override public void onSuccess(List&lt;Person&gt; people) { Log.d(TAG, "onSuccess: called"); currentList.setValue(people); // here I pass List to LiveData&lt;List&gt; } @Override public void onError(Throwable e) { Log.d(TAG, "onError: called"); Toast.makeText(application, "NO DATA", Toast.LENGTH_SHORT).show(); } }); // LOBNYA konets mGuy.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleObserver&lt;Person&gt;() { @Override public void onSubscribe(Disposable d) { Log.d(TAG, "onSubscribe(Disposable d): called"); composite.add(d); } @Override public void onSuccess(Person person) { Log.d(TAG, "onSuccess: called"); currentList.setValue(person); // there is an error, for I cannot bypass &lt;Person&gt; to LiveData&lt;List&lt;&gt;&gt; } @Override public void onError(Throwable e) { Log.d(TAG, "onError: called"); } }) } LiveData&lt;List&lt;Person&gt;&gt; getCurrentList() { return currentList; } };</pre> </div></list<object></object> - how to add and delete <Object> from (and to) LiveData<List<Object>>? 如何更改数组列表中的整数而不丢失其在该列表中的顺序 - How to change an integer from an arraylist without it losing its order in that list 如何根据列表顺序更改提交按钮Java? - how to change submit button according to list order Java? 将订单列表更改为西班牙语 - Change the order list to spanish in java 通过更新变量来更改 LiveData 源 - Change LiveData source by updating a variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM