【问题标题】:Android : getting data from cursorAndroid:从光标获取数据
【发布时间】:2013-07-26 11:38:10
【问题描述】:
运行查询后。 Cursor有这样的结果(我这里只给4个)
ID | Name | Topic|
------------------
1 | A | Poster
------------------
1 | B | Poster
------------------
2 | C | Presentation
-------------------------
2 | D | Presentation
现在,我只想在列表视图的同一个单元格上显示 A & B 和 C & D。我该怎么做?
有没有办法像这样获取数据。
【问题讨论】:
标签:
java
android
listview
cursor
【解决方案1】:
您可以将光标中的数据作为字符串获取并根据需要合并它们,然后再将它们保存到列表视图中。
String example = (cursor.getString(0) + "," + cursor.getString(1));
cursor.moveToPosition(id);
example = example + (cursor.getString(0) + "," + cursor.getString(1));
ArrayList<String> listItems = new ArrayList<String>();
listItems.add(example);
【解决方案2】:
您可以使用它对我有用的代码。
c.moveToFirst();
if (c != null) {
do {
for (int i = 0; i < c.getColumnCount(); i++) {
Log.e("", "" + c.getString(i));
}
}while (c.moveToNext());
}
【解决方案3】:
GROUP BY 您的 SQL 查询中的主题并手动连接 A、B。我不认为你可以直接使用光标
【解决方案4】:
您需要为 listitem 对象创建一个包含相同类型项目的 bean,然后为该项目开发一个自定义列表视图适配器以存档该目标。