Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

    }

}


}

}
like image 743
sagar Avatar asked Apr 08 '12 04:04

sagar


People also ask

How do I uncheck RadioGroup on android?

Android RadioButton has only two states: checked or unchecked. Apart from these, the radio button can't have any other value. You can't uncheck a checked RadioButton.

What is RadioGroup why we need to use it explain with example?

RadioGroup is a widget in android which is used to handle multiple radio buttons within the android application. We can add multiple radio buttons to our RadioGroup. We have seen how to use radio buttons in android. In this article, we will take a look at How to implement Radio Group in the android application.

How to initialize radio button in android?

Open “res/layout/main. xml” file, add “RadioGroup“, “RadioButton” and a button, inside the LinearLayout . Radio button selected by default. To make a radio button is selected by default, put android:checked="true" within the RadioButton element.

How to use radio buttons in android?

RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make checked radio button. Once a radio button is checked, it cannot be marked as unchecked by user. RadioButton is generally used with RadioGroup.


1 Answers

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

this.radioGroup.clearCheck();
this.radioGroup.setOnCheckedChangeListener(this);
like image 64
Samir Mangroliya Avatar answered Oct 20 '22 22:10

Samir Mangroliya