简体   繁体   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?

In Dao:在道:

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

In Repo:在回购:

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;
    }
}

In ViewModel:在视图模型中:

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;
    }
}

В Activity: В 活动:

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();
    }
}

The recyclerView (cardStackView) shows empty, though objects with "debil" statusname are there surely (when I call them using LiveData, they are shown in the view). recyclerView (cardStackView) 显示为空,尽管具有“debil”状态名称的对象肯定存在(当我使用 LiveData 调用它们时,它们会显示在视图中)。 I guess there is something I typed incorrectly in ViewModel, maybe not at the appropriate place.我想我在 ViewModel 中输入了错误的东西,可能不是在适当的地方。 Can't find.找不到。 Where the mistake can be and how to fix it?错误可能在哪里以及如何解决?

workList is changing afterwards and you are using it before its get updated. workList之后会发生变化,而您在更新之前正在使用它。 better make workList a LiveData and observe the changes.最好让workList成为LiveData并观察变化。 When data is set to to list you have to notify the adapter for changes.当数据设置为列表时,您必须通知适配器进行更改。

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) { }
                });
    }
}

Observe the LiveData in your component.观察组件中的LiveData

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

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

相关问题 为单个obj创建通用类,列表 <obj> 并且有一个objs? - Creating a generic class for single obj, list<obj> and has-a objs? 从obj2转换或创建obj1,其中obj1和obj2扩展相同的抽象类 - cast or create obj1 from obj2 where obj1 and obj2 extends same abstract class 我无法从一个卡片视图活动中导航到多个活动。 我可以导航到“仅1个” - I cannot navigate to multiple activities from a single Card View Activity. I can navigate to Only 1 无法将值从 Activity 移交给 Repository 以将其传递给 Dao 方法 - Cannot handover value from Activity to Repository to pass it into Dao method 选择时从可滚动列表中删除obj - Remove obj from scroll able list when selected 从列表中查找总和<obj>使用 Java8 流</obj> - Find total of a value from List<Obj> using Java8 streams Java8-从符合搜索条件的obj获取实例变量值的列表(字段名称列表) - Java8 - Get list of instance variable values from obj matching search criteria(list of field names) 我如何从地图中获取价值 <Object,List<Object> &gt; obj = new HashMap <Object,List<Object> &gt;(); - How can i retrieve value from Map<Object,List<Object>> obj=new HashMap<Object,List<Object>>(); 从@RestController返回Obj - Return Obj from @RestController 如何检查列表 <obj> 一片空白? - How to check if List<obj> is null?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM