SQLite 复杂查询和处理结果
晚上好(对我来说),我是 Emanuele,一位年轻的 Android 开发人员。 我想与您分享我的问题,因为我没有找到好的解决方案。
我有 3 个表(实际上是 5 个)
posts
categories
posts_categories
来创建帖子和类别之间的多对多关系
我创建了一个 ListFragment 来显示我的所有帖子及其类别。
所以我创建了这个查询(我没有编写所有代码:))
SELECT posts._id, posts.post_title, categories.category_name
FROM posts
LEFT OUTER JOIN posts_categories
ON posts_categories.post_id=posts.post_id
LEFT OUTER JOIN categories
ON posts_categories.category_id=categories.category_id;
因此,如果我有 2 个帖子,每个帖子有 2 个类别,则该查询将返回 4 条记录。我错了吗? 所以在我的 Cursor 中我有 4 条记录,但我真正需要的是从光标收集数据并仅显示我真正需要的数据。
在这种情况下,我无法使用 CursorAdapter 来显示我的数据,因为它将在 ListFragment 中插入 4 个项目。
我不想只加载帖子,而不是每个帖子加载类别,因为如果我有 100 个帖子,我将执行 1 个查询来选择所有帖子,并执行 100 个查询来选择类别。
我能做些什么?我需要专家的建议!处理这种情况的最佳方法是什么? 你有遇到过这个问题吗?
非常感谢。埃马努埃莱·里奇。
Good evening ( for me ), I'm Emanuele a young android developer.
I want to share with you my problem because I'm not finding a good solution.
I've 3 tables (in reality 5)
posts
categories
posts_categories
to create the many-to-many relation between posts and categories
I've created a ListFragment to show all my posts with their categories.
So I've created this query ( I don't write all the code :) )
SELECT posts._id, posts.post_title, categories.category_name
FROM posts
LEFT OUTER JOIN posts_categories
ON posts_categories.post_id=posts.post_id
LEFT OUTER JOIN categories
ON posts_categories.category_id=categories.category_id;
So if I have 2 post with 2 category each that query will return 4 records. Am I wrong?
So in my Cursor I have 4 record but what I really need is to collect datas from the cursor and display only what I really need.
In this case I cannot use a CursorAdapter to display my data because it will insert 4 item in the ListFragment.
And I don't want to load only post and than for each post load categories because if I have 100 posts I will do 1 query to select all posts and 100 query to select categories.
What can I do? I need an expert advice! Which is the best way to handle this situation?
Have you ever faced this problem?
Thank you very much. Emanuele Ricci.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您扩展 ArrayAdapter 并为列表中的每个结果提供您自己的 ArrayList 和您自己的对象。 ArrayAdapter 提供了一个名为 getView() 的函数,应该重写并自定义该函数以膨胀并显示您需要的内容。这应该就是您所需要的。
I suggest that you extend ArrayAdapter and provide your own ArrayList with your own Object for each result in your list. The ArrayAdapter provides a function called getView() which should be overridden and customized to inflate and display what you need. That should be all you need.
您是否尝试过带有 Filterable 的 CursorAdapter?这是[链接](http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/AutoComplete4.html)。希望这有帮助。
Did you tried CursorAdapter with Filterable. Here is [link] (http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/AutoComplete4.html). Hope this help.