SQLite的数据到RecyclerView [英] SQLite data to a RecyclerView

查看:779
本文介绍了SQLite的数据到RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该应用程序在一个SQLite数据库中的数据。用户界面主要是RecyclerView。现在的问题是如何最好地将数据从数据库传输到用户界面,同时保持了主线程?

The app has data in a SQLite database. The UI is primarily a RecyclerView. The question is how to best to transfer data from the database into the UI, whilst keeping off the main thread?

我原计划用一个CursorLoader,ContentProvider的,和RecyclerView。但看完周围看起来RecyclerView有外的开箱没有对光标提供的数据支持。荡。

I originally planned to use a CursorLoader, ContentProvider, and RecyclerView. But reading around it looks like RecyclerView has no out-of-the-box support for Cursor-supplied data. Dang.

这则给我留下了一些其他选项...

That then leaves me with a few other options...


  1. 的AsyncTask加载数据,把它放入模型对象,并传递到RecyclerView适配器。除了是丑陋的,它没有配置变化友好。

  1. AsyncTask to load the data, put it into model objects, and pass into the RecyclerView Adapter. Aside from being ugly, it isn't config-change friendly.

这是从SQL加载数据,并将其推入模型对象的定制装载机。

A custom Loader that loads the data from SQL and pushes it into model objects.

使用游标加载器,当它返回游标迭代来将数据推入模型对象。我怀疑这将发生在主线程,并可能损坏性能。

Use a Cursor loader, and when it returns the Cursor iterate through it to push the data into model objects. I suspect this would occur on the main thread and may damage performance.

使用奥托发送一个请求消息来请求数据,然后返回一个模型由返回消息对象集合。有可能是500〜对象,所以我想我可能宁愿滥用奥托这样做。

Use Otto to send a request message to request data, and then return a model objects collection by return message. There may be ~500 objects so I think I may rather abusing Otto doing this.

如果我使用的模型对象的集合,而不是一个游标我看到更少的利益ContentProvider的,我也失去了用户界面自动刷新数据的变化(这可能是有用的)的能力。

If I am using a collection of model objects instead of a Cursor I see less benefit to a ContentProvider, and I also lose the ability for the UI to auto-refresh on data changes (which may be useful).

这些选项没有吸引多少,有没有更好的办法?应用程序是在时间pressure所以不管它是需要相当快速实施。不幸的是,UI需要水平滚动只针对棒棒糖,所以RecyclerView似乎比ListView的一个更好的选择。

None of these options appeal much, is there a better way? The app is under time pressure so whatever it is needs to be fairly quick to implement. Unfortunately the UI needs to scroll horizontally and only targets Lollipop, so RecyclerView does seem a better bet than ListView.

推荐答案

使用这个简单的适配器 https://开头gist.github.com/Shywim/127f207e7248fe48400b ,或者你可以使用 android.support.v17.leanback.widget.ItemBridgeAdapter android.support.v17.leanback.widget。 CursorObjectAdapter 但是为什么使自己的生活困难?

use this simple adapter https://gist.github.com/Shywim/127f207e7248fe48400b, alternatively you could use android.support.v17.leanback.widget.ItemBridgeAdapter with android.support.v17.leanback.widget.CursorObjectAdapter but why to make own life harder?

这篇关于SQLite的数据到RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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