所有的元素被最后一个元素的HashMap的ArrayList取代 [英] All elements are replaced by last element in an arraylist of hashmap

查看:396
本文介绍了所有的元素被最后一个元素的HashMap的ArrayList取代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有值

问题Q-ID结果
Q1 1结果
Q2 2结果
...等等

我想通过调用一个函数来进行检索。所以我用HashMaps这样的ArrayList如下..

 公开的ArrayList<&HashMap的LT;字符串,字符串>> getAllQuestions(整数ID)
 {
     尝试
     {
         HashMap的<字符串,字符串> QuesList =新的HashMap<字符串,字符串>();
         ArrayList的<&HashMap的LT;字符串,字符串>> QuestionArrayList =新的ArrayList<&HashMap的LT;字符串,字符串>>();
         //选择所有查询
         字符串selectQuery =<这里取代;某些查询          光标= mDb.rawQuery(selectQuery,NULL);         //通过所有行循环,并增加列表
         如果(cursor.moveToFirst())
         {
             做
             {                 QuesList.put(ques_id,cursor.getString(2));
                 QuesList.put(ques_text,cursor.getString(8));
                 QuestionArrayList.add(QuesList);
                 Log.i(疑问句,cursor.getString(8));
             }而(cursor.moveToNext());
         }
         Log.i(检查,QUES返回列表);
         返回QuestionArrayList;     }
     赶上(的SQLException mSQLException)
     {
         Log.e(TAGgetTestData>>中+ mSQLException.toString());
         扔mSQLException;
     }
 }

现在的LogCat中显示,所有问题都在单独的时间成功地检索获取(由Log.i声明所示),但我磨片在结束时运行下面的循环中的所有元素都是由上次读取的问题所取代。任何帮助深表AP preciated。

 对(HashMap的<字符串,字符串> T:QuesList)
    {
        Log.d(那里,算+ t.getString());
        Log.i(测绘......,t.get(ques_id));
     }


解决方案

在调用添加方法,只有参照对象添加。所以,下一次你修改对象,参考指修改的对象和对象的老态不保留。

在你的情况,你必须创建新的对象,你想将它们添加到列表每次

  //遍历所有行循环,并增加列表
     如果(cursor.moveToFirst())
     {
         做
         {
             //创建地图的一个新的对象实例
             HashMap的<字符串,字符串> QuesList =新的HashMap<字符串,字符串>();             QuesList.put(ques_id,cursor.getString(2));
             QuesList.put(ques_text,cursor.getString(8));
             QuestionArrayList.add(QuesList);
             Log.i(疑问句,cursor.getString(8));
         }而(cursor.moveToNext());
     }

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

解决方案

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

这篇关于所有的元素被最后一个元素的HashMap的ArrayList取代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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