Cursor 如何循环放到Map里呢

angel725 2010-08-26 03:19:26
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();

while (result.moveToNext()) {
map.put("name", result.getString(result.getColumnIndex("name")));
map.put("address", result.getString(result.getColumnIndex("address")));
list.add(map);
}

SimpleAdapter listadapter = new SimpleAdapter(Show.this, list,
R.layout.show_result, new String[] { "name", "address" },
new int[] { R.id.show_name, R.id.show_address });

这样写,如果输出结果的话,得到的只有最后一条。重复x次(如果数据里有x条记录)


如何能输出全部记录呢??
...全文
323 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
mittyAngela 2012-10-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();

while (result.moveToNext()) {
map.put("name",……
[/Quote]
我也需要做一个返回Map的功能,请问楼主实现了吗?能讲解一下吗?
starssn 2010-08-27
  • 打赏
  • 举报
回复
不好意思, 我看错了。
angel725 2010-08-26
  • 打赏
  • 举报
回复
谢谢两位,可以了,只要换个顺序就行了。
cc191954 2010-08-26
  • 打赏
  • 举报
回复
在 while 里面 new HashMap

while (result.moveToNext()) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", result.getString(result.getColumnIndex("name")));
map.put("address", result.getString(result.getColumnIndex("address")));
list.add(map);
}
starssn 2010-08-26
  • 打赏
  • 举报
回复
那把后面的参数删了就行了, 关键是这个cursor取得正确与否, 你可以试一试再说嘛, 名字和地址都给你取号了, 你想往哪放, 就自己放呗。 我还给都写出来啊
angel725 2010-08-26
  • 打赏
  • 举报
回复
我在API上查到rawquery好像没有你写的这么多参数。

while也只是放到变量里,我要放到list.add(map);
里,在ListView显示。
starssn 2010-08-26
  • 打赏
  • 举报
回复
更正下, 那个查询的表, 是你的表 不是“phoneCalllog”

查询语句可以这样写:SELECT * FROM userinfo
starssn 2010-08-26
  • 打赏
  • 举报
回复
你看这样做行吗?
Cursor result = 
db.rawQuery("SELECT * FROM phoneCalllog ORDER BY callogDate DESC LIMIT ?,?",
new String[] { "name", "address" }, null, null, null, null,null);


取得的时候:
  while(result.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("number")); String address= cursor.getString(cursor.getColumnIndex("address"));
}
angel725 2010-08-26
  • 打赏
  • 举报
回复
Cursor result = db.query("userinfo",
new String[] { "name", "address" }, null, null, null, null,
null);
starssn 2010-08-26
  • 打赏
  • 举报
回复
把取得cursor 的那句 写出来, 我看看
纠结的木棉花 2010-08-26
  • 打赏
  • 举报
回复
这个有些复杂

80,354

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧