簡體   English   中英

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

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

在Java中,我們使用books[0]訪問Livedata<List<Book>>圖書的第一個元素,如何在Kotlin中做到這一點?

我已經嘗試了一切。

// Java代碼books[0];

//科特林// books[0]引發錯誤

未解決的參考。 由於接收者類型不匹配,以下候選者都不適用:@InlineOnly公共內聯運算符fun <@OnlyInputTypes K,V> Map.get(key:Int):Category? 在kotlin.collections中定義

您可以嘗試books.get(0)

Kotlin允許您對類型進行某些運算符,一種是中綴,例如表示表達式/值之間的+ 我認為該錯誤告訴您無法使用數組符號訪問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 中? 如何更改 LiveData 列表的順序? 帶有 Room 和 LiveData 的 MVVM - 如何從 LiveData 獲取列表 隨機化 LiveData <list<names></list<names> 如何在我的存儲庫中訪問我的 LiveData 模型的值 如何添加和刪除<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> 通過更改列表類型進行Android LiveData轉換 LiveData 的變化<list<obj> &gt; 不顯示在 recyclerView 中,它在 LiveData 上訂閱<list></list></list<obj> Android ROOM - 如何觀察 LiveData 變化(每次設置日歷時)並將 LiveData 列表結果發送到我的適配器? 如何使用 Room 數據庫從 LiveData 列表中檢索用戶以顯示在 Spinner 中
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM