1

Im new to Android..

How to convert the hashmap values into ArrayList.. My problem is... Now i have a hashmap which contains my all datas(questionid,question,answers with multiple options).

Now what i want to do is...i need to display the question in textview and answers in radiobuttons based upon questionid, And that i have next button which is for display the next question and answers based upon questionid.

So Meanwhile i click on next button i should get increment the questionid and display the question and answers, same as for previous button... Please help me. im new to android..thanks a lot in advance...

hashmap

          String questionid = c.getString(TAG_QUESID);
          String question = c.getString(TAG_QUES);
          String answer = c.getString(TAG_ANSW);
          HashMap<String, String> map = new HashMap<String, String>();
          map.put(TAG_QUESID, questionid);
          map.put(TAG_QUES, question);
          map.put(TAG_ANSW, answer);
          System.out.println("QuestionIDMap:"+map);

Layout

  <TextView
    android:id="@+id/que_txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="59dp"
    android:layout_marginTop="36dp"
    android:text="TextView" />

 <RadioGroup
     android:id="@+id/rdgroup"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"
     android:layout_below="@+id/que_txt"
     android:layout_marginTop="38dp"
     android:orientation="vertical" >

     <RadioButton android:id="@+id/RB1" android:text="button1"/>

     <RadioButton android:id="@+id/RB2" android:text="button2"/>

     <RadioButton android:id="@+id/RB3" android:text="button3"/>

     <RadioButton android:id="@+id/RB4" android:text="button4"/>
 </RadioGroup>

 <TextView
     android:id="@+id/rdtxt"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_below="@+id/rdgroup"
     android:layout_marginTop="38dp"
     android:text="Answer: Nothing is picked">

 </TextView>

 <Button
     android:id="@+id/prv_btn"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBottom="@+id/nxt_btn"
     android:layout_alignParentLeft="true"
     android:layout_marginLeft="18dp"
     android:text="Previous" />

 <Button
     android:id="@+id/nxt_btn"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentRight="true"
     android:layout_below="@+id/rdtxt"
     android:layout_marginRight="50dp"
     android:layout_marginTop="34dp"
     android:text="Next" />
God'sGrace
  • 171
  • 1
  • 5
  • 19

1 Answers1

-1

I fear your example might be a bit to complex to get a straight away answer. Moreover, I personally think that your design is flawed if I have understood your question correctly. Lets start with the individual questions and the simple answers.

How to convert the hashmap values into ArrayList[?][...]

Java HashMaps have a simple method called values() which will convert all values in your HashMap into a Collection. As you used generics, there shouldn't be a problem to do the following, assuming the map from your example:

ArrayList<String> valueList = map.values();

A similar method for keys exists in case you wonder. It is called keySet() and will return a Set which is a bit different than a Collection. To convert it to a list, I suggest you read "convert set to list without creating new list". Another relevant thread for your question would be "Java Hashmap values to Array" which deals with the values() method.

[...]how to get the hashmap values in separate string to set textview for question and radiotext for answers[?][...]

Your question is a bit unclear. Do you want to provide multiple answers? Your example does not clearly show it. Also, I do not believe a "radiotext" element exists in Android.

[...]i want increment(questionid) the arrayList when click on next button. How to do this?[...]

This question made me think that you have your questions predefined somewhere and want to fill the HashMap with the answers. I will explain how to do this best in my opinion. Should this not be the case, feel free to comment, providing a more accurate description of your needs and I'll get back to you.

In android, you have the possibility to predefine strings using the strings.xml file which resides in your res\values folder. What I would recommend is, if the questions do not change, define them there. Now, upon creating your layout, you can already set the questions in the layout XML file using the android:text element. You can use it as follows:

<TextView
    android:id="@+id/your_text_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Your question should go here!" />

You can of course also do this programaticaly when initialising the view using the methods below:

TextView yourTextView =(TextView) findViewById(R.id.your_text_view);
yourTextView.setText("Your question should go here!");

To get your answers, you want the user to hit a button after each answer or at the end of the questionnaire. This tutorial shows you how to do that. In the implementation of the OnEditorActionListener, you can then retrieve the contents of each field and put it in the array. As you do provide the IDs to your fields, there should not be any problem matching up questions and answers.

This leaves us with one problem, counting the questions, giving them an ID, and keeping track of the ID. This is a bit cumbersome with your design. Lets go over it:

HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_QUESID, questionid);
map.put(TAG_QUES, question);
map.put(TAG_ANSW, answer);

Using this design, your keys need to be unique. That means you can always only have one question and answer should the keys be constants. I would recommend you either use a complex object to store questions and answers and use a HashMap<String, QuestionnaireItem> for example where the String is your Id and the QuestionnaireItem is your complex object. Alternatively you can use a multi-layer HashMap but this would get confusing real fast; HashMap<String, HashMap<String, String>. As for counting, when you fill the map with answers, simple keep a counter and increment it every time you add an element.

I would be glad to help you out if you are able to provide a better problem description.

Update By the improved description I am able to provide more information.

First of all, you should have defined a TextView as well as a RadioGroup in your layout file. You should then have loaded the layout when creating your view. All basic Android stuff. For the following example I suppose that you have your questions and answers and just need to know how to set them and how to move on to the next set of question and answers. Here is how you do it.

When you inflated your layout, before exiting the onCreate(Bundle savedInstanceState) method, you want to set the initial question and answers. To do that, just get your TextView respectively your RadioButton using the findViewById(R.id.the_views_id) method. Do not forget a cast.

RadioButton radioButton1 = (RadioButton)findViewById(R.id.RB1);

respectively

TextViewradioButton1 = (TextView)findViewById(R.id.que_txt);

Now all you need to do is call the methods to set the text of the views, setText("Your string!")

Now, to proceed to the next question you can implement the following method as highlighted by this Radio Buttons tutorial.

public void onRadioButtonClicked(View view)
{
    counter++;
    nextQuestion(counter);
}

This assumes you have a static counter variable initialised to 0.

public void nextQuestion(counter)
{
    // Here you set the values of the buttons and textview to the values in the arrays
    // at the index provided by the counter. Alternatively you can use the 
    // String.parseInt(counter) method to get a string which you could use as the ID
    // of the question and therefore key to your HashMap.
}
Community
  • 1
  • 1
Eric Tobias
  • 3,225
  • 4
  • 32
  • 50
  • My problem is... now i have a hashmap which contains my all datas(questionid,question,answers with multiple options). now what i want to do is...i need to display the question in textview and answers in radiobuttons based upon questionid. And that i have next button which is for display the next question and answers based upon questionid, so while i click on next button i should get increment the questionid and display the question and answers, same as for previous button... Please help me. im new to android..thanks a lot in advance... – God'sGrace Jan 29 '13 at 07:31
  • String QUESTION=map.get(TAG_QUES).toString(); TextView txtque = (TextView) findViewById(R.id.que_txt); txtque.setText(QUESTION); Like this i set my textview its display the last question from the string not first question... – God'sGrace Jan 29 '13 at 07:37
  • That is not hard at all. Could you provide an example of your layout file? I will then craft an example using your IDs. – Eric Tobias Jan 29 '13 at 07:41
  • See Layout i update it..Same time if click on the radiobutton i get that radiobutton text to display down. please help me thanks a lot to you..frnd – God'sGrace Jan 29 '13 at 07:47
  • I updated the answer. It should be easily adaptable to your layout as I was typing away while you updated. One more thing, instead of using the callback for the RadioButton change, you can also implement an `OnclickListener` for your button using the same body I have in the `onRadioButtonClicked` method. It all depends on the behaiour you want your application to have. – Eric Tobias Jan 29 '13 at 07:58
  • thanks frnd...i paste my code can you edit my code and give. Im sorry im new to android so i cant get the next question concept...please help me by edit my code and give i learn it....please frnd.. – God'sGrace Jan 29 '13 at 08:08
  • As far as I understand your code, you are doing it wrong. You want to declare your HashMap outside of the scope of the function. that way you can access it from anywhere. I would also recommend dimply storing the JSON objects with the question ID as a key. You want to access the UI thread from outside of that function. – Eric Tobias Jan 29 '13 at 09:20
  • No. I am glad to answer any questions you have but I think your initial question was answered. I propose you think a bit about the separation of UI from background tasks, especially since there are restrictions regarding that in Android, and ask another, more specific question. – Eric Tobias Jan 29 '13 at 12:36