All elements are replaced by last element in an arraylist of hashmap

痴心易碎 提交于 2020-01-07 03:10:11

问题


I have values as

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

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

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

回答1:


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.

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



回答2:


The reason for this is that, inside your loop, you are only using a single instance of Questlist which is added in the QuestionArrayList.

Try moving

 HashMap<String,String> QuesList = new HashMap<String,String>();

inside the loop.

 do 
         {

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



回答3:


You don't need a list here, use the map to store all your key/value pairs:

QuesList.put(cursor.getString(2), cursor.getString(8));

Will store your questions in the map. You can then loop:

for(String q : QuesList.values()) {…}

Ps: your problem is that you only use one map and keep using the same key, overriding previous entries.



来源:https://stackoverflow.com/questions/12067579/all-elements-are-replaced-by-last-element-in-an-arraylist-of-hashmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!