数据与放大器之间的松耦合的最佳实践; UI Android中 - 适配器,过滤器,CursorLoader和ContentProvider的 [英] Best practice for loose coupling between data & UI in Android - Adapter, Filter, CursorLoader and ContentProvider

查看:226
本文介绍了数据与放大器之间的松耦合的最佳实践; UI Android中 - 适配器,过滤器,CursorLoader和ContentProvider的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个活动 N 的TextView S中的重present一行音符。这些记录保存在某个地方(本地数据库,网络等),每一次 onResume()被调用,适当数量的的TextView ,则根据所存储的数据绘制的。

Assume we have an Activity with n TextViews that represent one line notes. These notes are stored somewhere (local database, network etc), and each time onResume() being called, the proper number of TextViews are drawn according to that stored data.

现在,让我们说,用户要删除一个音符,这将是解决具体的最好办法的TextView ,回到它的存储实体?

Now, lets say the user want to delete a note, what would be the best way the resolve the specific TextView, back to its storage entity?

目前,我唯一知道的方法是使用 View.Tag ,并且有一些经理将其翻译为数据实体,但它看起来相当凌乱。

At the moment, the only way I know is by using View.Tag, and having some manager to translate it to data entity, but it look rather messy.

是否还有其他选择吗?

推荐答案

在Android上,适配器动作视图和数据模型之间的桥梁。您可以显示在 N TextViews 在任何一个的ListView GridView控件,当用户添加或删除音符,在本地或服务器数据库第一次更新。在web服务调用和/或本地数据库更新完成后,新的数据被添加到底层适配器。在查看然后通过调用刷新 adapter.notifyDataSetChanged()。这将是做到这一点的方式。

In Android, the Adapter acts a bridge between the view and the data model. You could display the n TextViews in either a ListView or a GridView, and when the user adds or deletes a note, the local or server database is first updated. Upon completion of the web service call and/or the local database update, the new data is added to the underlying Adapter. The View is then refreshed by calling adapter.notifyDataSetChanged(). This would be the way to do it.

的方法:


  • 如果更新本地的SQLite 数据库,你可以考虑使用
    CursorAdpater
    保存数据的查看,因为它直接的条目中映射
    本地数据库到查看

  • 如果利用的的ContentProvider ,它甚至可以组合
    一个的CursorAdapter
    LoaderManager

    CursorLoader
    这些插入活动 / 片段生命周期和监控
    底层的ContentProvider 对于那些发布的更改
    自动给查看上一个单独的线程。

  • 另外,也可以使用一个
    过滤器
    适配器一起来定义一个动态机制,
    排序上即时的数据条目。过滤由执行
    过滤器上一个单独的线程,根据用户输入的查询,
    可能在
    AutoCompleteTextView

  • If updating the local SQLite database, you could consider using a CursorAdpater to hold the data for the View, as it directly maps the entries in the local database to the View.
  • If making use of a ContentProvider, it is even possible to combine a CursorAdapter with a LoaderManager and a CursorLoader: these plug into the Activity / Fragment life-cycle and monitor the underlying ContentProvider for changes that are published automatically to the View on a separate thread.
  • It is also possible to use a Filter in conjunction with the Adapter to define a dynamic mechanism that sorts the data entries on-the-fly. The filtering is performed by the Filter on a separate thread, as per a query entered by the user, possibly in an AutoCompleteTextView.

参考文献:

  • See the Retrieving a List of Contacts tutorial. The example here retrieves a set of contacts from the contacts ContentProvider based on a dynamic, alphabetical search by the user. It makes use of CursorAdapter, CursorLoader and LoaderManager to monitor and update the data, and it displays the search results in a ListView.
  • See also the Android Realtime (Instant) Search with Filter Class example, which shows how a Filter is to be used.
  • Android AutoCompleteTextView with Custom Adapter filtering.
  • Android AutocompleteTextView using ArrayAdapter and Filter.

这篇关于数据与放大器之间的松耦合的最佳实践; UI Android中 - 适配器,过滤器,CursorLoader和ContentProvider的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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