开发者

Android HashMap shows only last item

开发者 https://www.devze.com 2023-04-13 07:15 出处:网络
So I\'m getting data from Database and put it into a HashMap and trying to show it on listview. But the problem is that it\'s shows only the last item as text of all textviews and I really can\'t find

So I'm getting data from Database and put it into a HashMap and trying to show it on listview. But the problem is that it's shows only the last item as text of all textviews and I really can't find where is my mistake. So If anyone can help me I will be very happy. Here is the code I'm using :

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;
    Str开发者_开发技巧ing 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 is that you initialized your Hash map inside of for loop so it will get re-initialized every time loop iterates .

So put hm = new HashMap<String, Object>();

outside of Loop , also try to use this way for getting values from DB while using Cursor

Try this code for Adding values from DB to Hash Map :

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


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

give those statements you create a different hashmap for every row of you query results. is it really what you want? Second.. with hashmap, every time you use the same key the old value is replaced with the more recent. So you can use an index like:

    int i = 0;
   while (I HAVE ROW) {
       hm.put(IMAGE+i, b);
       hm.put(TITLE+i, text);
}


You should declare hm inside of the for loop. When you do it like this:

@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);
       }
       ...

It adds the reference of hashmap to items, but you are changing that reference on every iteration. So essentially you are adding the same reference over and over again. You shoud do it like this:

@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);
        }
       ...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号