当每个对象位于 Cursor 中的多行时,在 ListView 中显示数据

发布于 2024-10-11 05:07:42 字数 897 浏览 1 评论 0原文

在我的应用程序中,我有一个如下所示的 sqlite 数据库:

CREATE TABLE notes (_id integer primary key, 
                    content text);
CREATE TABLE tags (_id integer primary key,
                   name text,
                   noteid integer,
                   foreign key(noteid) references notes(_id));

我正在存储可以有一些与之关联的标签的文本。现在我想在 ListView 中显示此文本和标签。但是我不知道如何使用 SimpleCursorAdapter 来做到这一点。有可能吗?我的数据可能如下所示:

sqlite> select * from notes;
1|foo bar baz
sqlite> select * from tags;
1|x|1
2|y|1

获取所有笔记的查询及其返回的数据如下所示:

sqlite> select notes._id, notes.content, tags.name from notes, tags where notes._id = tags.noteid;
1|foo bar baz|x
1|foo bar baz|y

现在,如果我想以某种方式将此数据绑定到 ListView ,该怎么做?如果 ListView 中的每一行包含两行,一行包含内容,一行包含所有标签,我会很高兴。我的猜测是 SimpleCursorAdapter 对我没有帮助吗?我该怎么办?

In my application I have a sqlite database that looks like this:

CREATE TABLE notes (_id integer primary key, 
                    content text);
CREATE TABLE tags (_id integer primary key,
                   name text,
                   noteid integer,
                   foreign key(noteid) references notes(_id));

I'm storing text that can have some tags associated with it. Now I want to show this text and the tags in a ListView. However I can't figure out how to do this with a SimpleCursorAdapter. Is it even possible? My data might look like this:

sqlite> select * from notes;
1|foo bar baz
sqlite> select * from tags;
1|x|1
2|y|1

The query to get all notes and the data it returns looks like this:

sqlite> select notes._id, notes.content, tags.name from notes, tags where notes._id = tags.noteid;
1|foo bar baz|x
1|foo bar baz|y

Now, if I want to bind this data to the ListView in some way, how to do it? I would be happy if each row int the ListView contained two lines, one line with the content and one line with all the tags. Am I correct in guessing that the SimpleCursorAdapter won't help me here? What should I do instead?

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

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

发布评论

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

评论(1

一张白纸 2024-10-18 05:07:42

SimpleCursorAdapter 无法为您提供帮助。

如果您的目标是希望一行成为一个注释+其所有标签,您可以尝试覆盖 SimpleCursorAdapter 中的 bindView() 并以这种方式倒入标签。这意味着您已经构建了某种 note->tags 的 HashMap,因此可以快速确定要放入该行的标签。

要构建 HashMap,我认为您有两种选择:

  1. 通过在 HashMap 中查找注释来动态构建它们,然后执行查询如果未找到该注释,则获取该注释的标签,并将它们缓存在 HashMap 中以供以后重用(例如,滚动)。这里的问题是,您正在执行一堆小查询(不好),并在用户滚动时在主应用程序线程上执行它们(非常糟糕)。

  2. 使用 IN 子句执行一次大型查询,获取所有笔记的所有标签,并将生成的 Cursor 转换为完全填充的 HashMap。然后,您的每行查找都会成功。如果您只有少量的行,那么这种方法很有效。否则,此查询可能需要比用户耐心等待的时间更长的时间。

如果您的架构很灵活,您可能会考虑是否可以更好地进行一定程度的非规范化,例如通过逗号分隔列表或其他方式将标签放在注释表的单列中。即使这会使写入操作变得复杂(例如,将标签放在两个位置),如果您的读取数量大大超过写入数量,那么这可能是值得的。

SimpleCursorAdapter alone can't help you here.

If your goal is that you want one row to be one note + all its tags, you can try overriding bindView() in SimpleCursorAdapter and pouring in the tags that way. That would imply that you have already built up some sort of HashMap of note->tags and therefore can quickly determine the tags to go in the row.

To build up the HashMap, you have two choices that I see:

  1. Build them on the fly by looking up the note in the HashMap, then doing a query to get the tags for that note if they're not found, caching them in the HashMap for later reuse (e.g., scrolling). The catch here is that you're doing a bunch of little queries (bad) and doing them on the main application thread while the user is scrolling (really bad).

  2. Do one big query using an IN clause to get all tags for all notes, and convert the resulting Cursor into a fully-populated HashMap. Then, your per-row lookups will all succeed. This works well if you only have a modest number of rows; otherwise, this query may take longer than the user has patience for.

If your schema is flexible, you might consider whether you are better served with some amount of denormalization, such as having the tags in a single column of the notes table via a comma-delimited list or something. Even if that complicates write operations (e.g., putting tags in two places), if your reads greatly outnumber your writes, it may be worth it.

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