所有元素都被 hashmap 的数组列表中的最后一个元素替换 [英] All elements are replaced by last element in an arraylist of hashmap

查看:23
本文介绍了所有元素都被 hashmap 的数组列表中的最后一个元素替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的价值观是

问题Q-id
Q1 1
Q2 2
...等等

Question Q-id
Q1 1
Q2 2
...and so on

我想通过调用一个函数来检索它们.所以我使用了一个 HashMaps 的数组列表,如下所示..

I want to retrieve them by calling a function. So i used an arraylist of HashMaps as follows..

public ArrayList<HashMap<String,String>> getAllQuestions(Integer id)
 {
     try
     {
         HashMap<String,String> QuesList = new HashMap<String,String>();
         ArrayList<HashMap<String, String>> QuestionArrayList = new ArrayList<HashMap<String, String>>();
         // Select All Query
         String selectQuery = <some query here>;

          cursor = mDb.rawQuery(selectQuery, null);

         // looping through all rows and adding to list
         if (cursor.moveToFirst()) 
         {
             do 
             {

                 QuesList.put("ques_id", cursor.getString(2));
                 QuesList.put("ques_text", cursor.getString(8));
                 QuestionArrayList.add(QuesList);
                 Log.i("ques",cursor.getString(8) );
             } while (cursor.moveToNext());
         }


         Log.i("check"," Ques list returned");
         return QuestionArrayList;

     }
     catch (SQLException mSQLException) 
     {
         Log.e(TAG, "getTestData >>"+ mSQLException.toString());
         throw mSQLException;
     }
 }

现在 Logcat 显示在单独提取时成功检索了所有问题(如 Log.i 语句所示),但是当我在最后运行以下循环时,所有元素都被最后提取的问题替换.非常感谢任何帮助.

Now the Logcat shows that all questions are retrieved successfully at the time of individual fetch (as shown by the Log.i statement) but whe i run the following loop at the end all the elements are replaced by the last fetched question. Any help is much appreciated.

   for(HashMap<String, String> t : QuesList )
    {
        Log.d("out there", "count" + t.getString());
        Log.i("mapping....",t.get("ques_id"));
     }

推荐答案

调用 add 方法时,只会添加对对象的引用.所以下次修改对象时,引用指向修改后的对象,不保留对象的旧状态.

When you call the add method, only the reference to the object is added. So next time you modify the object, the reference refers to the modified object and the old state of the object is not retained.

在您的情况下,每次要将新对象添加到 List 时,您都必须创建新对象:

In your case, you will have to create new objects every time you want to add them to the List:

     // looping through all rows and adding to list
     if (cursor.moveToFirst()) 
     {
         do 
         {
             //Create a new object instance of the Map
             HashMap<String,String> QuesList = new HashMap<String,String>();

             QuesList.put("ques_id", cursor.getString(2));
             QuesList.put("ques_text", cursor.getString(8));
             QuestionArrayList.add(QuesList);
             Log.i("ques",cursor.getString(8) );
         } while (cursor.moveToNext());
     }

这篇关于所有元素都被 hashmap 的数组列表中的最后一个元素替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆