Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8412585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:42:31+00:00 2026-06-10T00:42:31+00:00

I have values as Question Q-id Q1 1 Q2 2 …and so on I

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:42:33+00:00Added an answer on June 10, 2026 at 12:42 am

    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());
         }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sorry about the naive question. I have the following: public Array Values {
i have a question how to select certain values from a table. I have
Beginner question: I have a dictionary where the values are lists of (a variable
I have a question about SqlDataReader: Is there some way to modificated the values
I have a very simple question, How can I get the POST values from
my question is how to insert values into DB in sql. i have a
I have some questions about the default values in a function parameter list Is
This question is because I have some values in a database table that works
I have #define values in headers that I certainly want Doxygen to document but
[Part 1 - Original question] I have values like 30, 31, 28 or 29

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.