【问题标题】:All elements are replaced by last element in an arraylist of hashmap所有元素都被hashmap的arraylist中的最后一个元素替换
【发布时间】:2012-08-22 06:40:51
【问题描述】:

我的价值观是

问题 Q-id
第一季度 1
第二季度 2
...等等

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

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 语句所示),但是当我在最后运行以下循环时,所有元素都被最后一个提取的问题替换。非常感谢任何帮助。

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

【问题讨论】:

    标签: android arraylist hashmap


    【解决方案1】:

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

    在你的情况下,每次你想将它们添加到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());
         }
    

    【讨论】:

    • 虽然是问题,但还有一个更根本的问题:一开始就不应该有列表。
    • @assylias:我不太确定。该问题没有说明如何使用生成的数据结构。也许消费 API 需要这种布局。
    • @JanHudec 这是一种可能性,但我认为值得一提,以防不是原因。
    • @assylias 除了这个,你还有什么建议。我想将这些值传递给另一个活动,然后用它们的 id 一次显示一个。
    【解决方案2】:

    这样做的原因是,在您的循环中,您只使用了一个添加到 QuestionArrayList 中的 Questlist 实例。

    尝试移动

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

    在循环内部。

     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】:

      这里不需要列表,使用映射来存储所有键/值对:

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

      将您的问题存储在地图中。然后你可以循环:

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

      Ps:您的问题是您只使用一个地图并继续使用相同的键,覆盖以前的条目。

      【讨论】:

      • 问题中的结构确实看起来不是最理想的,但它可能是使用 API 所需要的,或者列表中的每个映射中可能有更多条目,或者条目的顺序可能很重要或某些其他事情,所以你的建议可能有效也可能无效。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      • 2021-08-10
      • 1970-01-01
      • 2014-06-02
      相关资源
      最近更新 更多