所以我从数据库中获取数据并将其放入HashMap并尝试在listview上显示它。但问题是,它只显示最后一项作为所有textview的文本,我真的找不到我的错误在哪里。所以,如果有人能帮助我,我将非常高兴。这是我正在使用的代码:
public class Recommended extends TabGroupActivity {
private final String IMAGE = "";
private final String TITLE = "";
private final String CARDS_COUNT = "";
private ArrayList <HashMap<String, Object>> items;
Bitmap b;
String cardsCount;
String text;
int collId;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.recommended_layout);
ListView lv1 = (ListView)findViewById(R.id.listViewRecommended);
UserDatabaseHelper userDbHelper = new UserDatabaseHelper(this, null, 1);
userDbHelper.initialize(this);
items = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> hm;
String sql = "SELECT objectId, title, cardsCount FROM collections WHERE isRecommended = 1";
Cursor cursor = userDbHelper.executeSQLQuery(sql);
if(cursor.getCount()==0){
Log.i("Cursor Null","CURSOR IS NULL");
} else if(cursor.getCount()>0){
for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
text = cursor.getString(cursor.getColumnIndex("title"));
Log.i("Show Title","Show Title : "+text);
cardsCount = cursor.getString(cursor.getColumnIndex("cardsCount"));
collId = Integer.parseInt(cursor.getString(cursor.getColumnIndex("objectId")));
Log.i("CollId","Collection ID : "+collId);
b = BitmapFactory.decodeFile("/sdcard/7073d92dce10884554d7e047f1c51cb6.jpg", null);
hm = new HashMap<String, Object>();
hm.put(IMAGE, b);
hm.put(TITLE, text);
hm.put(CARDS_COUNT, cardsCount +" Stampii");
items.add(hm);
}
Log.i("items", "items: " + items);
final SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.main_listview,
new String[]{TITLE, CARDS_COUNT, IMAGE}, new int[]{ R.id.main_name, R.id.main_info, R.id.main_img});
lv1.setAdapter(adapter);
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Intent previewMessage = new Intent(Recommended.this, MyCollectionId.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
previewMessage.putExtra("collection_id", collId);
parentActivity.startChildActivity("MyCollectionId", previewMessage);
}
});
}
答案 0 :(得分:3)
问题是你在for循环中初始化了你的Hash映射,所以每次循环迭代都会重新初始化它。
所以放hm = new HashMap<String, Object>();
在Loop之外,也尝试使用这种方式在使用Cursor
时从DB获取值尝试使用此代码将数据库中的值添加到哈希映射:
hm = new HashMap<String, Object>();
if (cursor.moveToFirst()) {
do {
text = cursor.getString(cursor.getColumnIndex("title"));
Log.i("Show Title","Show Title : "+text);
cardsCount = cursor.getString(cursor.getColumnIndex("cardsCount"));
collId = Integer.parseInt(cursor.getString(cursor.getColumnIndex("objectId")));
Log.i("CollId","Collection ID : "+collId);
b = BitmapFactory.decodeFile("/sdcard/7073d92dce10884554d7e047f1c51cb6.jpg", null);
hm.put(IMAGE, b);
hm.put(TITLE, text);
hm.put(CARDS_COUNT, cardsCount +" Stampii");
items.add(hm);
} while (cursor.moveToNext());
}
答案 1 :(得分:1)
hm = new HashMap<String, Object>();
hm.put(IMAGE, b);
hm.put(TITLE, text);
hm.put(CARDS_COUNT, cardsCount +" Stampii");
items.add(hm);
为这些语句提供您为查询结果的每一行创建不同的哈希映射。它真的是你想要的吗?第二..使用hashmap,每次使用相同的键时,旧值将替换为更新的值。所以你可以使用像这样的索引:
int i = 0;
while (I HAVE ROW) {
hm.put(IMAGE+i, b);
hm.put(TITLE+i, text);
}
答案 2 :(得分:1)
你应该在for循环中声明hm。当你这样做时:
@Override
public void onCreate(Bundle savedInstanceState){
....
HashMap<String, Object> hm;
....
for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
//do something with hm
items.add(hm);
}
...
它将hashmap的引用添加到项目中,但是您将在每次迭代时更改该引用。所以基本上你一遍又一遍地添加相同的参考。你应该这样做:
@Override
public void onCreate(Bundle savedInstanceState){
....
for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
HashMap<String, Object> hm;
//do something with hm
items.add(hm);
}
...