当每个对象位于 Cursor 中的多行时,在 ListView 中显示数据
在我的应用程序中,我有一个如下所示的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅
SimpleCursorAdapter
无法为您提供帮助。如果您的目标是希望一行成为一个注释+其所有标签,您可以尝试覆盖
SimpleCursorAdapter
中的bindView()
并以这种方式倒入标签。这意味着您已经构建了某种 note->tags 的HashMap
,因此可以快速确定要放入该行的标签。要构建
HashMap
,我认为您有两种选择:通过在
HashMap
中查找注释来动态构建它们,然后执行查询如果未找到该注释,则获取该注释的标签,并将它们缓存在HashMap
中以供以后重用(例如,滚动)。这里的问题是,您正在执行一堆小查询(不好),并在用户滚动时在主应用程序线程上执行它们(非常糟糕)。使用
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()
inSimpleCursorAdapter
and pouring in the tags that way. That would imply that you have already built up some sort ofHashMap
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: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 theHashMap
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).Do one big query using an
IN
clause to get all tags for all notes, and convert the resultingCursor
into a fully-populatedHashMap
. 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.