Dhruvil
Dhruvil

Reputation: 49

have made radio button dynamically but sometime first radio button is not able to select

while selecting first radio button its not showing its checked.. but when i select other and after that if i select first it shows its checked.

Code :-

 private void Displayradiobutton(String hatchfetchnumber) {

    DBAdapter db = new DBAdapter(getApplicationContext());
    Log.i("test", "SETTING ADAPTER FOR DISPLAYRADIOBUTTON()");
    Cursor cr;
    cr = db.gethatchno(hatchfetchnumber);
    Log.i("test", "GET DATA FROM GETHATCHNO" + cr);
    cr.moveToFirst();
    selecthatch = cr.getInt(0);
    Log.i("test", "Step 2");
    if (selecthatch >= 12) {
        down.setVisibility(View.VISIBLE);
    } else {
        down.setVisibility(View.INVISIBLE);
    }
    errortext.setVisibility(View.VISIBLE);
    radiogroup.removeAllViews();

    for (int i = 1; i <= selecthatch; i++) {

        RadioButton rdbtn = new RadioButton(this);
        rdbtn.setId(i);
        rdbtn.setPadding(15, 7, 15, 7);
        rdbtn.setTextSize(20);
        rdbtn.setText("Hatch " + i);

        radiogroup.addView(rdbtn);
        text.setEnabled(true);
        if (selecthatch == 1) {
            rdbtn.setChecked(true);
            flag = 1;
        } else {
            flag = 0;
        }

    }

}

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
    flag = 1;
    errortext.setVisibility(View.INVISIBLE);
    // selectedHatch = (RadioButton) findViewById(checkedId);
}}

Upvotes: 0

Views: 58

Answers (1)

Gopal Gopi
Gopal Gopi

Reputation: 11131

try this...

change

    if (selecthatch == 1) {
        rdbtn.setChecked(true);
        flag = 1;
    } else {
        flag = 0;
    }

to

    if (i == 1) {
        rdbtn.setChecked(true);
        flag = 1;
    } else {
        flag = 0;
    }

Upvotes: 1

Related Questions