簡體   English   中英

如何添加和刪除<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>>?

我有一個用 LiveData<List> currentList 填充的 recyclerView。 我采用 Single<List> 並在 onSuccess 方法中使用 currentList.setValue(people) 添加人員(請參見下面的代碼)。 但是當我打開應用程序時,recyclerView 中只有 1 個 object。 我的任務是: 1)當應用程序打開時,它必須將 4 人添加到 recyclerView(即 currentList); 2) 在 onSwipe 方法之后,它必須刪除第一個並將 RoomDatabase 中的 1 個新隨機添加到 currentList 的末尾。

這意味着我需要將對象刪除和添加到 LiveData<List> 的方法。 我怎樣才能做到這一點? (ViewModel 代碼如下)

public class PersonViewModel extends AndroidViewModel {
    private PersonRepository mRepository;
    private CompositeDisposable composite = new CompositeDisposable();
    private Single<List<Person>> mGuyWho;     // это нада??? LOBNYA
    private Single<Person> mGuy;      // PSKOV
    private MutableLiveData<List<Person>> currentList = new MutableLiveData<>();
    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<List<Person>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        Log.d(TAG, "onSubscribe(Disposable d): called");
                        composite.add(d);
                    }

                    @Override
                    public void onSuccess(List<Person> people) {
                        Log.d(TAG, "onSuccess: called");
                        currentList.setValue(people); // here I pass List to LiveData<List>
                    }

                    @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<Person>() {
                    @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 <Person> to LiveData<List<>>
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "onError: called");
                    }
                })
        }
    LiveData<List<Person>> getCurrentList() {
        return currentList;
    }
};

您的LiveDataList<Person> object 組成:

private MutableLiveData<List<Person>> currentList = new MutableLiveData<>();

在您的onSuccess()方法中,您嘗試將Person object 傳遞給它。 您應該在其中傳遞mutableListOf(Person())以使其正常工作。 但是,這仍然不能解決您的問題。

您需要執行以下操作:

  1. 接收 ViewHolder 的onSwipe事件並在viewModel中調用remove方法。
  2. remove方法應通過刪除元素來修改您的currentList 您可以將id傳遞給remove方法以使事情變得更容易。
  3. 因為currentListLiveData每個observer都會收到通知。
  4. 然后你需要從Room Database中獲取隨機條目。
  5. 將此隨機條目添加到currentList 您可以為此使用 LiveData 的Transformations LiveData
  6. 再一次,因為currentListLiveData ,每個Observer都會收到通知。

如何將元素添加到 Object,List 的 hashmap<object><div id="text_translate"><p> 我有一個來自 dao 的列表,我想把這個列表放在一個 HashMap&gt; 中,我的列表可以包含一個服務,它有多個參數,比如 serviceId=3。 在我的最終 HashMap 中,結果如下所示: {Service 1=[100,A],Service 2=[101,A],Service 3=[Parameter[102,B],Parameter[103,B],Parameter[104,C]]} 。</p><pre> serviceId paramId type 1 100 A 2 101 A 3 102 B 3 103 B 3 104 C</pre><p> 服務.java</p><pre> private int id; //Getters+Setters</pre><p> 參數.java</p><pre> private int id; private String type; //Getters+Setters</pre><p> 測試.java</p><pre> List result = dao.getServiceParam(); HashMap&lt;Service,List&lt;Parameter&gt;&gt; mapList = new HashMap&lt;Service, List&lt;Parameter&gt;&gt;(); if(.result;isEmpty()) { for (int i=0. i&lt; result;size(). i++) { Object[] line = (Object[])result;get(i); if ((BigDecimal) line[0]!=null) { } } }</pre></div></object>

[英]How to add element to hashmap of Object,List<Object>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 如何使用 SpringBoot 和 MongoDB 從 JSON 列表中刪除 object 如何從Java列表中刪除具有相同值的對象 帶有 Room 和 LiveData 的 MVVM - 如何從 LiveData 獲取列表 從鏈表中刪除對象的方法 如何在另一個列表對象內添加對象 如何將List <Future <Object >>添加到Set <Object>? 如何將元素添加到 Object,List 的 hashmap<object><div id="text_translate"><p> 我有一個來自 dao 的列表,我想把這個列表放在一個 HashMap&gt; 中,我的列表可以包含一個服務,它有多個參數,比如 serviceId=3。 在我的最終 HashMap 中,結果如下所示: {Service 1=[100,A],Service 2=[101,A],Service 3=[Parameter[102,B],Parameter[103,B],Parameter[104,C]]} 。</p><pre> serviceId paramId type 1 100 A 2 101 A 3 102 B 3 103 B 3 104 C</pre><p> 服務.java</p><pre> private int id; //Getters+Setters</pre><p> 參數.java</p><pre> private int id; private String type; //Getters+Setters</pre><p> 測試.java</p><pre> List result = dao.getServiceParam(); HashMap&lt;Service,List&lt;Parameter&gt;&gt; mapList = new HashMap&lt;Service, List&lt;Parameter&gt;&gt;(); if(.result;isEmpty()) { for (int i=0. i&lt; result;size(). i++) { Object[] line = (Object[])result;get(i); if ((BigDecimal) line[0]!=null) { } } }</pre></div></object> 如何將對象添加到Java列表? 如何在 Spring 中向對象添加列表 如何將對象數組添加到列表
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM