如何从房间数据库中读取所有值并显示到recyclerview中? [英] how to read all value from room data base and show into recyclerview?

查看:357
本文介绍了如何从房间数据库中读取所有值并显示到recyclerview中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用会议室数据库从URL存储json数据,但工作正常,但是在从数据库访问数据以在recyclerview上显示时,一次给出一个结果,如果我第二次打开应用程序,则显示第二个对象,我正在房间里的新物品可以有人建议我该怎么做,我的代码是. 应用我的pojo课. 我的适配器类是

I am using room database for storing json data from URL its working fine but while accessing data from data base to show on recyclerview its give one result at time and if i am open app second time then second object is showing, i am new in room can any one suggest me how to do this my code is. App my pojo class. My Adapter Class is.

   recyclerView = view.findViewById(R.id.recyclerView);
    postsAdapter = new PostsAdapter(new ArrayList<Example>());
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    addBorrowViewModel = ViewModelProviders.of(this).get(AddBorrowViewModel.class);
    addBorrowViewModel.getItemAndPersonList().observe(getActivity(), new Observer<List<Example>>() {
        @Override
        public void onChanged(@Nullable List<Example> itemAndPeople)
        {
            recyclerViewAdapter.addAll(itemAndPeople);
        }
    });

推荐答案

按照

您没有在dao中添加LiveData.添加livedata,它将自动为您返回列表. 但是

You didn't add LiveData in your dao. Add livedata and it will automatically return the list for you. However

在你的身边

@Query("select * from table_name")
public LiveData<List<Model>> getAll();

在您的活动或片段中

 yourViewModel.getAll().observe(this, new Observer<List<Model>>() {
        @Override
        public void onChanged(@Nullable List<Model> models) {
            adapter.addAll(models);
        }
    });

这篇关于如何从房间数据库中读取所有值并显示到recyclerview中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆