已动态制作单选按钮但有时第一个单选按钮无法选择

时间:2013-11-25 12:27:06

标签: android

选择第一个单选按钮时,它没有显示已选中..但是当我选择其他时,如果我先选择它,则显示已选中。

代码: -

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

1 个答案:

答案 0 :(得分:1)

试试这个......

更改

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

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