2011-10-13 61 views
0

所以我從數據庫中獲取數據並將其放入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); 
       } 
      }); 
} 

回答

3

問題是,你初始化你的哈希表內的循環,這樣它會被重新初始化,每次循環迭代。

所以把hm = new HashMap<String, Object>();

循環之外,還嘗試用這種方式從數據庫獲取值,而使用遊標

試試這個代碼從數據庫中添加值散列映射:

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()); 
} 
+0

我已經嘗試......而其實我修復problem.I'm真的不知道爲什麼,但之後,我把一些文字在初始化OT TITLE,CARDS_COUNT它工作大聲笑 –

1
   hm = new HashMap<String, Object>(); 
       hm.put(IMAGE, b); 
       hm.put(TITLE, text); 
       hm.put(CARDS_COUNT, cardsCount +" Stampii"); 
       items.add(hm);    

給那些語句創建爲您查詢結果的每一行不同的HashMap中。它真的是你想要的嗎?其次..使用hashmap,每次使用相同的鍵時,舊值將被替換爲更新的值。因此,您可以使用如下索引:

int i = 0; 
    while (I HAVE ROW) { 
     hm.put(IMAGE+i, b); 
     hm.put(TITLE+i, text); 
} 
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); 
     } 
     ...