为列表视图中的每个项目加载子记录

发布于 2024-12-15 23:33:58 字数 535 浏览 1 评论 0原文

我正在制作一个应用程序,用户可以在其中将简单的类似时间表的交易记录到列表中。我已经使用列表视图完成了此操作,该列表视图通过自定义 CursorAdapter 后代连接到 sqlite 查询。到目前为止,它运行良好。

接下来我想做的是让用户向单个交易添加任意数量的“标签”(想想 gmail 中针对电子邮件的标签)。我有一个多对关系表来将标签链接到事务,但我不确定如何修改现有的列表视图和光标排列,以便每个单独的记录都可以加载和显示其标签。

我想到的一件事是将所有数据加载到两层对象结构中,然后将其用作列表视图的数据源。但我不喜欢这个想法,因为它肯定会导致加载延迟,而使用 CursorAdapter 似乎对性能更好。

我打算在每个列表视图项中显示标签的方式就像纯文本一样,使用 Html.fromHtml() 以不同颜色沿着行底部线性显示。我已经让这部分工作了,所以我希望坚持使用现有的列表视图(如果可能)。我的问题纯粹是找到从数据库中获取标签并使它们可用于列表视图中的每一行的最佳和最有效的方法。

我对 Android 很陌生 - 有没有一种聪明的方法来做到这一点,或者这将是一个手动练习?

I'm making an app in which the user can log simple timesheet-like transactions into a list. I have done this with a listview which is connected to a sqlite query with a custom CursorAdapter descendent. So far it is working well.

What I am trying to do next is let the user add any number of "tags" to an individual transcation (think labels against emails in gmail). I have a many-many-relationship table to link tags to transactions, but i'm unsure how to modify my existing listview and cursor arrangement so that each individual record can load and display its tags.

One thing that comes to mind is to load all the data into a two-tiered object structure, then use that as the datasource for the listview. I don't like this idea though because it will certainly cause a delay in loading, whereas using a CursorAdapter seems to be better for performance.

The way I was intending to display the tags within each listview item was just as plain text, linearly along the bottom of the row with different colours using Html.fromHtml(). I already have this part working so i was hoping to stick with the existing listview (if possible). My problem is purely about finding the best and most efficient way of getting the tags out of the DB and making them available to each row in the listview.

I'm very new to android - is there a clever way to do this or is it going to be a manual exercise?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二手情话 2024-12-22 23:33:58

这没有捷径。您必须通过子类化 LinearLayoutRelativeLayout 或任何您想要的内容来创建 View。在您的 View 类中公开一个公共方法,例如“setTag(tag)”,该方法采用一个参数来设置相应数据的标签。在您的View中,您必须有一些字段来维护对该View的引用。换句话说,在 Adapter 的 getView 中返回视图之前,您可以将数据绑定到 View,然后在 View 内更改其状态。

There's no shortcut for this. You're going to have to create a View by subclassing LinearLayout, RelativeLayout or whatever you want. In your View class expose a public method like 'setTag(tag)' which takes an argument as to what tag to set the corresponding data to. In your View you'll have to have some field which maintains a reference to that View. In other words, somewhere before you return your view in getView in your Adapter, you can bind the data to the View and then alter its state from within the View.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文