0

I make some culinary application on android and there is any quiz within, for the question i've been able to get the text from array to text field, but for the answer i don't know how to display it to radio button. I'm new to android, please help me and thank you in advance.

cuisine1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:src="@drawable/papeda128" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="40dp"
        android:text="Papeda"
        android:textColor="#000000"
        android:textSize="30dp" />
</LinearLayout>

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="30dp"
            android:text="Question"
            android:textColor="#000000"
            android:textSize="20dp" />

    </LinearLayout>
</ScrollView>

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:checked="true"
        android:text="Answer 1" />

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Answer 2" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Answer 3" />
</RadioGroup>

</LinearLayout>

cuisine1.java

package com.culinary;

import java.util.ArrayList;
import java.util.Collections;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;


public class cuisine1 extends Activity {

 TextView papquest;
 private int i=0;;
 question q=new question();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cuisine1);

    papquest=(TextView)findViewById(R.id.textView2);
    String dt=q.showquestionpapeda(i);
    String[] quest=dt.split("/");
    papquest.setText(quest[0],null);


    findViewById(R.id.radio0).setOnClickListener(mClickListener);
    findViewById(R.id.radio1).setOnClickListener(mClickListener);
    findViewById(R.id.radio2).setOnClickListener(mClickListener);

}   

    RadioGroup.OnClickListener mClickListener = new RadioGroup.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            i+=1;

             String dt=q.showquestionpapeda(i);
             String[] quest=dt.split("/");
             papquest.setText(quest[0],null);

            }

    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

}

question.java

package com.culinary;

class question {
    String[] papeda={
            "Question 1 ?/Answer1,Answer2,Answer3",
            "Question 2 ?/Answer1,Answer2,Answer3",
            "Question 3 ?/Answer1,Answer2,Answer3",
            "Question 4 ?/Answer1,Answer2,Answer3",

    };

public void question(){

    }

public String showquestionpapeda(int i){
        String question;
        question=papeda[i];
        return question;
    }
}
kokodroid
  • 1
  • 1

2 Answers2

0

You can do the following:

//Get your string   
String dt=q.showquestionpapeda(i);
// First split on basis of '/'
String [] firstSplit = dt.split("/");
// Get the second element consisting of "Answer1,Answer2,Answer3" string and split based on ","
String [] secondSplit = firstSplit[1].split(",");

//get the radio group and set the text of first second and third radio button
RadioGroup rbtnGrp = (RadioGroup)findViewById(R.id.radioGroup1);
for (int i = 0; i < rbtnGrp.getChildCount(); i++) {
    // setting the text on the three children with three strings in the array
    ((RadioButton) rbtnGrp.getChildAt(i)).setText(secondSplit[i]); 
}

You can also set the text outside loop for each of them individually. Hope this helps.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
0

you have to do something like the following :

// this get the number of radioButtons inside the radioGroup
int count = radioGroup.getChildCount();
// this is what you are doing to split your quesitons 
 String[] quest=dt.split("/");

//here we loop through all the child radioButtons 
    for (int i=0;i<count;i++) {
//get the radio button at the index i
        View o = radioGroup.getChildAt(i);
//if the view is a RadioButton 
        if (o instanceof RadioButton) {
            o.setText(quest[i]);
        }
    }

and please give me somefeed back

Hope that helps .