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

sagar picture sagar · Apr 8, 2012 · Viewed 11.9k times · Source

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;
    }

    }

}


}

}

Answer

Samir Mangroliya picture Samir Mangroliya · Apr 8, 2012

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

this.radioGroup.clearCheck();
this.radioGroup.setOnCheckedChangeListener(this);