假设我们有一个活动
与 N
的TextView
S中的重present一行音符。这些记录保存在某个地方(本地数据库,网络等),每一次 onResume()
被调用,适当数量的的TextView
,则根据所存储的数据绘制的。
Assume we have an Activity
with n
TextView
s that represent one line notes. These notes are stored somewhere (local database, network etc), and each time onResume()
being called, the proper number of TextView
s 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
.
参考文献:
查看 检索列表联系人 教程。这里的例子检索一组从触点联系人 ContentProvider的基于动态的,按字母顺序搜索
用户。它利用的CursorAdapter
, CursorLoader
和 LoaderManager
来监控和更新数据,并显示了搜索结果在的ListView
。又见 Android的实时(即时)与过滤器类搜索 的例子,它展示了如何在过滤器
将被使用。 的Android AutoCompleteTextView使用自定义过滤适配器 。 的Android AutocompleteTextView使用ArrayAdapter和过滤器 。
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.