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

查看:28
本文介绍了如何从房间数据库中读取所有值并显示到 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);
        }
    });

推荐答案

Follow 这个这个

您没有在您的 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天全站免登陆