reset my checked radiogroup when the radiogroup is used again in the same activity
Asked Answered
Y

2

7

I am making a quiz application...I am accessing the questions through "id"...every time a question is answered(any radio button is checked),the id is incremented and doInBackground() function is called to load the next question....But when the next question is loaded i want my radiogroup be completely refreshed(no radio checked,original white color text) ... how to do that????

here is my activity....

 public class JsonDemo extends Activity 
 {
JSONObject json;
HttpClient client;
TextView q_desc,c_or_w,id_check;
RadioButton rb_a,rb_b,rb_c,rb_d;
RadioGroup rg;
String ans;
int id=1;
int score=0;




@Override
protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub

    super.onCreate(savedInstanceState);

    setContentView(R.layout.result_json);

    q_desc=(TextView)findViewById(R.id.q_desc);
    c_or_w=(TextView)findViewById(R.id.corr_incorrect);
    id_check=(TextView)findViewById(R.id.id_check);
    rg=(RadioGroup)findViewById(R.id.rg_option);
    rb_a=(RadioButton)findViewById(R.id.opt_a);
    rb_b=(RadioButton)findViewById(R.id.opt_b);
    rb_c=(RadioButton)findViewById(R.id.opt_c);
    rb_d=(RadioButton)findViewById(R.id.opt_d);

    client=new DefaultHttpClient();


    new Read().execute(Integer.toString(id));

}
 public  JSONObject getData(String id)throws     ClientProtocolException,IOException,JSONException
   {
     String url = "http://10.0.2.2:7001/proj/json.jsp";
     HttpPost post = new HttpPost(url);
     List<NameValuePair> pairs = new ArrayList<NameValuePair>();
     pairs.add(new BasicNameValuePair("id",id));
     post.setEntity(new UrlEncodedFormEntity(pairs));

    HttpResponse r=client.execute(post);
    int status=r.getStatusLine().getStatusCode();

    if(status == 200)
    {
        HttpEntity e=r.getEntity();
        String data=EntityUtils.toString(e);
        JSONObject last=new JSONObject(data);
        return last;


    }
    else
    {
        Toast.makeText(JsonDemo.this, "error", Toast.LENGTH_SHORT);
        return null;
    }

}



 public class Read extends AsyncTask<String,Integer,JSONObject> implements     OnCheckedChangeListener
{
ProgressDialog dialog = ProgressDialog.show(JsonDemo.this, "", "Loading Question,   Please wait...");

@Override
protected void onPreExecute() {


    dialog.show();

}

@Override
protected void onProgressUpdate(Integer... values) {
    // TODO Auto-generated method stub
    rg.clearCheck();
}

@Override
protected void onPostExecute(JSONObject result) {
    // TODO Auto-generated method stub
    try {
        String desc=json.getString("desc");
        String option_a=json.getString("a");
        String option_b=json.getString("b");
        String option_c=json.getString("c");
        String option_d=json.getString("d");
        ans=json.getString("ans");
        q_desc.setText(desc);
        rb_a.setText(option_a);
        rb_b.setText(option_b);
        rb_c.setText(option_c);
        rb_d.setText(option_d);
        rg.clearFocus();
        if(dialog!=null)
        {
            dialog.dismiss();
        }


        rg.setOnCheckedChangeListener(this);

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }

 @Override
 protected JSONObject doInBackground(String... params) {
    try {
        json=getData(params[0]);
        return json;
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
 }

 public void onCheckedChanged(RadioGroup rg, int checkedId) {

    switch(checkedId)
    {
    case R.id.opt_a:
    {
        if(ans.equalsIgnoreCase("a"))
        {

            id=id+1;
            c_or_w.setText("Correct");
            id_check.setText(Integer.toString(id)); 
            rb_a.setTextColor(Color.parseColor("#33ff99"));
            if(id>10)
            {

            }
            else{
                score=score+1;
            new Read().execute(Integer.toString(id));
            }
        }
        else
        {
            id=id+1;
            c_or_w.setText("InCorrect");
            id_check.setText(Integer.toString(id));
            if(id>10)
            {

            }
            else{
            new Read().execute(Integer.toString(id));
            }

        }
        break;

    }
    case R.id.opt_b:
    {
        if(ans.equalsIgnoreCase("b"))
        {

            id=id+1;
            c_or_w.setText("Correct");
            id_check.setText(Integer.toString(id));
            rb_b.setTextColor(R.color.green);
            if(id>10)
            {

            }
            else{
                score=score+1;
            new Read().execute(Integer.toString(id));
            }   
        }
        else
        {
            id=id+1;
            c_or_w.setText("InCorrect");
            id_check.setText(Integer.toString(id));
            if(id>10)
            {

            }
            else{

            new Read().execute(Integer.toString(id));
            }   
            }
        break;
    }
    case R.id.opt_c:
    {
        if(ans.equalsIgnoreCase("c"))
        {

            id=id+1;
            rb_a.setTextColor(666);
            c_or_w.setText("Correct");
            id_check.setText(Integer.toString(id));         
            if(id>10)
            {

            }
            else{
                score=score+1;
            new Read().execute(Integer.toString(id));
            }
        }
        else
        {
            id=id+1;
            c_or_w.setText("InCorrect");
            id_check.setText(Integer.toString(id));
            if(id>10)
            {

            }
            else{
            new Read().execute(Integer.toString(id));
            }

        }
        break;
    }
    case R.id.opt_d:
    {
        if(ans.equalsIgnoreCase("d"))
        {

            id=id+1;
            c_or_w.setText("Correct");
            id_check.setText(Integer.toString(id)); 
            if(id>10)
            {

            }
            else{
                score=score+1;
            new Read().execute(Integer.toString(id));
            }
        }
        else
        {
            id=id+1;
            c_or_w.setText("InCorrect");
            id_check.setText(Integer.toString(id));
            if(id>10)
            {

            }
            else{

            new Read().execute(Integer.toString(id));
            }

        }
        break;
    }

    }

}


}

}
Yeasty answered 8/4, 2012 at 4:40 Comment(0)
Z
23

I think you should clear check to your RadioGroup in onPostExecute()

this.radioGroup.clearCheck();
this.radioGroup.setOnCheckedChangeListener(this);
Zollie answered 8/4, 2012 at 4:49 Comment(4)
but when i use this ...in postExecute().. the next question is loaded with rg unchecked... but the next question is skipped.. and the rd question is loaded...so every time one of the question is left unanswered....Do u have ne other suggestion..where to keep clearCheck() function....Yeasty
@Yeasty what happened ? i cant understand your commentZollie
i mean to say...when a question is displayed...I click on a radio button..now the next question should be displayed....BUT the next to next question gets displayed(the question in the middle gets omitted)....these things happen if i use the "rg.clearCheck()" any where....The thing about which i was asking in my comment was.. is there any other place or new methor where i can use rg.clearCheck() and donot face the above problem...Yeasty
I got an idea!!!!! I will make another activity...same as JsonDemo.. with a little changes.. Json Demo activity Call the new Activity after changing the score and id, pass their values to the new activity...The new activity, now will load the next question with new radiogroup ...after processing it will pass the incremented id and score to JsonDemo...My problem gets solved... :)Yeasty
T
0

If anyone still trying to find a solution for similar problem without having to create new activity, try setting every radio button id everytime you re-use the radio group.

RadioButton radioButton = findViewById(R.id.radio_button);
radioButton.setId(0);
Tampere answered 10/3, 2021 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.