所以我從數據庫中獲取數據並將其放入HashMap並嘗試在列表視圖中顯示它。但問題是,它只顯示所有文本瀏覽的文本的最後一個項目,我真的找不到我的錯誤在哪裏。所以如果有人能幫助我,我會很開心。下面是我使用的代碼:Android HashMap只顯示最後一項
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);
}
});
}
我已經嘗試......而其實我修復problem.I'm真的不知道爲什麼,但之後,我把一些文字在初始化OT TITLE,CARDS_COUNT它工作大聲笑 –