【发布时间】:2013-09-04 11:53:14
【问题描述】:
我想对来自两个游标的结果进行排序。
让我们考虑两个游标是cursor1 和cursor2。
Cursor1 用于table X,Cursor2 用于table Y。
Cursor1 使用 date 类型的列 Start 对数据进行排序,Cursor2 使用 date 类型的列 Date 对数据进行排序。
有些字段在两个表中是通用的,有些则不是。
但是,两个表之间没有绝对关系。
问题很明显。合并后的数据应该是两个游标的排序列表。
现在发生的事情是:
我从光标Cursor1 获得排序列表,它独立于来自光标Cursor2 的排序列表。
如何合并生成的游标数据以便按两个日期 (Start and Date) 获取排序列表?
例如:
我得到了这个结果:
| Date | Type | Location |
|:---------------------|------------:|:---------------------:|
| 10-Jul-2013 07:05:00 | Random | Washougal, Washington
| 10-Jul-2013 08:30:00 | Single | Vancouver, Washington
| 10-Jul-2013 07:30:00 | Multiple | Vancouver, Washington
| 10-Jul-2013 15:31:00 | Double | Vancouver, Washington
前两行来自表 X,最后两行来自表 Y。
但我想要这个结果:
| Date | Type | Location |
|:---------------------|------------:|:---------------------:|
| 10-Jul-2013 07:05:00 | Random | Washougal, Washington
| 10-Jul-2013 07:30:00 | Multiple | Vancouver, Washington
| 10-Jul-2013 08:30:00 | Single | Vancouver, Washington
| 10-Jul-2013 15:31:00 | Double | Vancouver, Washington
查询应该是:
Cursor1 = Select alpha, beeta, gamma, Remark, id, number from X order by Start ASC
Cursor2 = Select Type, Date, gamma, Location, Obs, number from Y order by Date ASC
从 Cursor1 得到结果后,我在这样的循环中创建字符串 html:
String itemList = "";
itemList += "<tr><td align='left' width='28%' style='font-size:8px;padding-left: 15px'>"
+ cursor1.getString(1)
+ "</td><td width='16%' style='font-size:8px'>"
+ cursor1.getString(0)
+ "</td><td width='10%' style='font-size:8px'>"
+ cursor1.getString(2)
+ "</td><td width='20%' style='font-size:8px'>"
+ "</td><td width='35%'>" + cursor1.getString(3) + "</td></tr>";
然后将此 itemList 进一步添加到来自 Cursor2 的结果中,以完成两个游标的 itemList。
最终的 itemList 在布局中显示为 html。
【问题讨论】:
-
可以连接两个表,但是如果你不告诉我们两个源的结构和结果应该是什么样子,没有人可以帮助你。
标签: android database sqlite sorting cursor