public List<Map<String, Object>> findsousuosearch(String search) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
Cursor cursor = db.rawQuery("SELECT pm.id,pm.cname,pm.cname_pywb,pm.spunit,pm.price," +
"pmo.photo,pmo.comment FROM posbase_menu as pm,posbase_menu_ole as pmo where pmo.masterid=pm.id and pm.cname_pywb like 'B%'",null);
while (cursor.moveToNext()) {
int id =cursor.getInt(cursor.getColumnIndex("id"));
String cname = cursor.getString(cursor.getColumnIndex("cname"));
String cname_pywb = cursor.getString(cursor.getColumnIndex("cname_pywb"));
String spunit = cursor.getString(cursor.getColumnIndex("spunit"));
int price=cursor.getInt(cursor.getColumnIndex("price"));
byte[] photo =cursor.getBlob(cursor.getColumnIndex("photo"));
String comment=cursor.getString(cursor.getColumnIndex("comment"));
map.put("backimg", id);
map.put("cname", cname);
map.put("cname_pywb", cname_pywb);
map.put("spunit", spunit);
map.put("price", price);
map.put("photo", photo);
map.put("comment", comment);
list.add(map);
}
cursor.close();
return list;
}
但是查询的时候获取的就是最后一条添加的数据
我不知道错在了哪里。。
然后我又从网上查了一下 设置map初始化的时候我放在了最外面
然后我是这样设置的
public List<Map<String, Object>> findsousuosearch(String search) {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = null;
Cursor cursor = db.rawQuery("SELECT pm.id,pm.cname,pm.cname_pywb,pm.spunit,pm.price," +
"pmo.photo,pmo.comment FROM posbase_menu as pm,posbase_menu_ole as pmo where pmo.masterid=pm.id and pm.cname_pywb like 'B%'",null);
while (cursor.moveToNext()) {
int id =cursor.getInt(cursor.getColumnIndex("id"));
String cname = cursor.getString(cursor.getColumnIndex("cname"));
String cname_pywb = cursor.getString(cursor.getColumnIndex("cname_pywb"));
String spunit = cursor.getString(cursor.getColumnIndex("spunit"));
int price=cursor.getInt(cursor.getColumnIndex("price"));
byte[] photo =cursor.getBlob(cursor.getColumnIndex("photo"));
String comment=cursor.getString(cursor.getColumnIndex("comment"));
map=new HashMap<String, Object>();
map.put("backimg", id);
map.put("cname", cname);
map.put("cname_pywb", cname_pywb);
map.put("spunit", spunit);
map.put("price", price);
map.put("photo", photo);
map.put("comment", comment);
list.add(map);
}
cursor.close();
return list;
}
把map初始化放在while里面 就可以了,
这样就保存了所有的数据了