Android 所有元素都将替换为hashmap的arraylist中的最后一个元素

Android 所有元素都将替换为hashmap的arraylist中的最后一个元素,android,arraylist,hashmap,Android,Arraylist,Hashmap,我有自己的价值观 问题Q-id 问题1 Q2 2 ……等等 我想通过调用函数来检索它们。因此,我使用了如下哈希映射的数组列表 public ArrayList<HashMap<String,String>> getAllQuestions(Integer id) { try { HashMap<String,String> QuesList = new HashMap<String,String>();

我有自己的价值观

问题Q-id
问题1
Q2 2
……等等

我想通过调用函数来检索它们。因此,我使用了如下哈希映射的数组列表

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;
     }
 }
public ArrayList getAllQuestions(整数id)
{
尝试
{
HashMap QuesList=新的HashMap();
ArrayList QuestionArrayList=新的ArrayList();
//选择所有查询
字符串selectQuery=;
cursor=mDb.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst())
{
做
{
QuesList.put(“ques_id”,cursor.getString(2));
QuesList.put(“ques_text”,cursor.getString(8));
问题列表。添加(问题列表);
Log.i(“ques”,cursor.getString(8));
}while(cursor.moveToNext());
}
Log.i(“检查”,“返回的问题列表”);
返回问题列表;
}
catch(SQLException-msqleexception)
{
Log.e(标记“getTestData>>”+msqleexception.toString());
抛出mSQLException;
}
}
现在,Logcat显示,在单独获取时(如Log.i语句所示),所有问题都已成功获取,但当我在最后运行以下循环时,所有元素都将替换为最后获取的问题。非常感谢您的帮助

   for(HashMap<String, String> t : QuesList )
    {
        Log.d("out there", "count" + t.getString());
        Log.i("mapping....",t.get("ques_id"));
     }
for(HashMap t:QuesList)
{
Log.d(“在那里”,“count”+t.getString());
Log.i(“映射…”),t.get(“ques_id”);
}

调用
add
方法时,只添加对对象的引用。因此,下次修改对象时,引用将引用已修改的对象,并且不会保留对象的旧状态

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

     // 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());
     }
//遍历所有行并添加到列表
if(cursor.moveToFirst())
{
做
{
//创建地图的新对象实例
HashMap QuesList=新的HashMap();
QuesList.put(“ques_id”,cursor.getString(2));
QuesList.put(“ques_text”,cursor.getString(8));
问题列表。添加(问题列表);
Log.i(“ques”,cursor.getString(8));
}while(cursor.moveToNext());
}

调用
add
方法时,只添加对对象的引用。因此,下次修改对象时,引用将引用已修改的对象,并且不会保留对象的旧状态

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

     // 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());
     }
//遍历所有行并添加到列表
if(cursor.moveToFirst())
{
做
{
//创建地图的新对象实例
HashMap QuesList=新的HashMap();
QuesList.put(“ques_id”,cursor.getString(2));
QuesList.put(“ques_text”,cursor.getString(8));
问题列表。添加(问题列表);
Log.i(“ques”,cursor.getString(8));
}while(cursor.moveToNext());
}

原因是,在循环中,您只使用添加到QuestionArrayList中的Questlist的单个实例

试着移动

 HashMap<String,String> QuesList = new HashMap<String,String>();
HashMap QuesList=newhashmap();
在循环内部

 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());
do
{
HashMap QuesList=新的HashMap();
QuesList.put(“ques_id”,cursor.getString(2));
QuesList.put(“ques_text”,cursor.getString(8));
问题列表。添加(问题列表);
Log.i(“ques”,cursor.getString(8));
}while(cursor.moveToNext());

原因是,在循环中,您只使用添加到QuestionArrayList中的Questlist的单个实例

试着移动

 HashMap<String,String> QuesList = new HashMap<String,String>();
HashMap QuesList=newhashmap();
在循环内部

 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());
do
{
HashMap QuesList=新的HashMap();
QuesList.put(“ques_id”,cursor.getString(2));
QuesList.put(“ques_text”,cursor.getString(8));
问题列表。添加(问题列表);
Log.i(“ques”,cursor.getString(8));
}while(cursor.moveToNext());

此处不需要列表,请使用地图存储所有键/值对:

QuesList.put(cursor.getString(2), cursor.getString(8));
将在地图中存储您的问题。然后可以循环:

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

Ps:您的问题是,您只使用一个映射并一直使用同一个键,覆盖以前的条目。

您不需要在此处列出列表,请使用映射存储所有键/值对:

QuesList.put(cursor.getString(2), cursor.getString(8));
将在地图中存储您的问题。然后可以循环:

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

Ps:您的问题是,您只使用一个映射,并一直使用同一个键,覆盖以前的条目。

虽然这是问题所在,但还有一个更根本的问题:首先不应该有列表。@assylias:我不太确定这一点。问题没有说明如何使用生成的数据结构。也许消费API需要这种布局。@JanHudec这是一种可能性,但我认为值得一提的是,如果这不是原因的话。@assylias除此之外,你还有什么建议。我想将这些值放到另一个活动中,然后用它们的id一次显示一个。虽然这是个问题,但还有一个更根本的问题:首先不应该有列表。@assylias:我不太确定这一点。问题没有说明如何使用生成的数据结构。也许消费API需要这种布局。@JanHudec这是一种可能性,但我认为值得一提的是,如果这不是原因的话。@assylias除此之外,你还有什么建议。我想将这些值放到另一个活动中,然后显示它们