简体   繁体   English

如何访问“ LiveData”列表中的元素“ Livedata <List<Model> &gt;在科特林?

[英]How to access the elements of the LiveData list "Livedata<List<Model>> in Kotlin?

In Java we access the first element of Livedata<List<Book>> books using books[0] , how to do that in Kotlin? 在Java中,我们使用books[0]访问Livedata<List<Book>>图书的第一个元素,如何在Kotlin中做到这一点?

I have already tried everything. 我已经尝试了一切。

//Java code books[0]; // Java代码books[0];

//Kotlin // books[0] throws error //科特林// books[0]引发错误

Unresolved reference. 未解决的参考。 None of the following candidates is applicable because of receiver type mismatch: @InlineOnly public inline operator fun <@OnlyInputTypes K, V> Map.get(key: Int): Category? 由于接收者类型不匹配,以下候选者都不适用:@InlineOnly公共内联运算符fun <@OnlyInputTypes K,V> Map.get(key:Int):Category? defined in kotlin.collections 在kotlin.collections中定义

You could try books.get(0) 您可以尝试books.get(0)

Kotlin allows you to some operators on types, one is infix, such as the + as it is between to expresses/values. Kotlin允许您对类型进行某些运算符,一种是中缀,例如表示表达式/值之间的+ I think the error is telling you you cant access books using array notation. 我认为该错误告诉您无法使用数组符号访问books

如何添加和删除<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 <List<User> &gt; 到 LiveData <List<String> &gt; 在 ViewModel 中? - How to transform LiveData<List<User>> to LiveData<List<String>> in ViewModel? 如何更改 LiveData 列表的顺序? - How to change the order of a LiveData List? 带有 Room 和 LiveData 的 MVVM - 如何从 LiveData 获取列表 - MVVM with Room and LiveData - How to get List from LiveData 随机化 LiveData <list<names></list<names> - Randomize LiveData<List<names> 如何在我的存储库中访问我的 LiveData 模型的值 - How can I access the value of my LiveData model in my respository 如何添加和删除<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>>? 通过更改列表类型进行Android LiveData转换 - Android LiveData Transformation by changing list type LiveData 的变化<list<obj> &gt; 不显示在 recyclerView 中,它在 LiveData 上订阅<list></list></list<obj> - Changes in LiveData<List<Obj>> aren't shown in recyclerView, which is subscrived on the LiveData<List> Android ROOM - 如何观察 LiveData 变化(每次设置日历时)并将 LiveData 列表结果发送到我的适配器? - Android ROOM - How can I Observe LiveData changes (every time my calendar is set-up) and send the LiveData list results to my adapter? 如何使用 Room 数据库从 LiveData 列表中检索用户以显示在 Spinner 中 - How to retrieve users from a LiveData List to display in a Spinner using Room Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM