簡體   English   中英

無法移交列表<obj> (來自單<list<obj> &gt;) 從 ViewModel 到 Activity。 RecyclerView 保持為空。 原因在哪里? </list<obj></obj>

[英]Cannot handover List<Obj> (got from Single<List<Obj>>) from ViewModel to Activity. RecyclerView stays empty. Where is the cause?

在道:

    @Query("SELECT * FROM person_table WHERE status = :statusname ORDER BY RANDOM() LIMIT 1")
Single<List<Person>> getGuyWho(String statusname);

在回購:

public class PersonRepository {
    private Single<List<Person>> mGuyWho; 
    PersonRepository(Application application) {
        PersonRoomDatabase db = PersonRoomDatabase.getDatabase(application);        
        mPersonDao = db.PersonDao();
        mGuyWho = mPersonDao.getGuyWho("debil"); 
    }
    Single<List<Person>> getGuyWho() {
        return mGuyWho;
    }
}

在視圖模型中:

public class PersonViewModel extends AndroidViewModel {
    private PersonRepository mRepository;
    CompositeDisposable composite = new CompositeDisposable();
    private Single<List<Person>> mGuyWho; 
    private List<Person> workList;
    public PersonViewModel(@NonNull Application application) {
        super(application);
        mRepository = new PersonRepository(application);
        workList = new ArrayList<>(); //initializing workList
        mGuyWho = mRepository.getGuyWho();

        mGuyWho.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new SingleObserver<List<Person>>() {
                @Override
                public void onSubscribe(Disposable d) {
                    composite.add(d);
                }

                @Override
                public void onSuccess(List<Person> people) {
                    Log.d(TAG, "onSuccess: called");
                    workList.addAll(people);
                }

                @Override
                public void onError(Throwable e) {
                    Log.d(TAG, "onError: called");
                    Toast.makeText(application, "NO DATA", Toast.LENGTH_SHORT).show();
                }
            });
    }
    public List<Person> getWorkList() {
        return workList;
    }
}

В 活動:

public class MudakActivity extends AppCompatActivity implements CardStackListener {
    List<Person> debilList;
    private PersonViewModel debilViewModel;
    private CardStackView debilCardStackView;
    private MudakAdapter debilAdapter;
    //declaring variables

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        debilAdapter = new DebilAdapter(new DebilAdapter.PersonDiff(), debilList); 
        debilCardStackView.setLayoutManager(debilCardStackLayoutManager);
        debilCardStackView.setAdapter(debilAdapter);
        //initializing other variables
        debilViewModel = new ViewModelProvider(this,
                ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication()))
                .get(PersonViewModel.class);
        debilList = debilViewModel.getWorkList();
    }
}

recyclerView (cardStackView) 顯示為空,盡管具有“debil”狀態名稱的對象肯定存在(當我使用 LiveData 調用它們時,它們會顯示在視圖中)。 我想我在 ViewModel 中輸入了錯誤的東西,可能不是在適當的地方。 找不到。 錯誤可能在哪里以及如何解決?

workList之后會發生變化,而您在更新之前正在使用它。 最好讓workList成為LiveData並觀察變化。 當數據設置為列表時,您必須通知適配器進行更改。

public class PersonViewModel extends AndroidViewModel {
    private PersonRepository mRepository;
    CompositeDisposable composite = new CompositeDisposable();
    private Single<List<Person>> mGuyWho;
    public LiveData<List<Person>> workList = new MutableLiveData<>();
    public PersonViewModel(@NonNull Application application) {
        super(application);
        mRepository = new PersonRepository(application);
        workList = new ArrayList<>(); //initializing workList
        mGuyWho = mRepository.getGuyWho();
        mGuyWho.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SingleObserver<List<Person>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        composite.add(d);
                    }
                    @Override
                    public void onSuccess(List<Person> people) {
                        workList.setValue(people);
                    }
                    @Override
                    public void onError(Throwable e) { }
                });
    }
}

觀察組件中的LiveData

debilViewModel.workList.observe(this, (Observer<List<Person>>) personList -> {
        debilAdapter.submitList(personList);
    });

暫無
暫無

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM