1

I have a doubt... In my Android app, I have an Activity that its GUI is created dynamically from certain data inside my SQLite database... I didn't had any kind of trouble with this and is working fine...

In this activity, there are a bunch of TextView's and RadioGroup's and RadioButton's that are created inside a for loop and the amount of RadioButton's varies due to a condition inside this loop... like this:

for(int i=0;i<numFilasFormulario;i++){

            TextView pregunta = new TextView(this);
            pregunta.setText(c.getString(c.getColumnIndex("texto")));
            pregunta.setGravity(Gravity.LEFT);
            pregunta.setTextColor(Color.rgb(0, 0, 0));
            pregunta.setTextSize(15);
            pregunta.setLayoutParams(params);
            ll.addView(pregunta);

            if(c.getString(c.getColumnIndex("tipopregunta")).equals("Si o No")){

                RadioGroup rg = new RadioGroup(this);
                ll.addView(rg);

                RadioButton b1 = new RadioButton(this);
                b1.setText("SI");
                rg.addView(b1);

                RadioButton b2 = new RadioButton(this);
                b2.setText("NO");
                rg.addView(b2);

            }else{

                if(c.getString(c.getColumnIndex("tipopregunta")).equals("Seleccion Simple")){

                    RadioGroup rg = new RadioGroup(this);
                    ll.addView(rg);

                    RadioButton b1 = new RadioButton(this);
                    b1.setText("SI");
                    rg.addView(b1);

                    RadioButton b2 = new RadioButton(this);
                    b2.setText("NO");
                    rg.addView(b2);

                    RadioButton b3 = new RadioButton(this);
                    b3.setText("N/A");
                    rg.addView(b3);

                }
            }
            c.moveToNext();
        }

So my question is how to obtain the value of the RadioButton selected by the user... I mean, do I have to invoke the method setOnClickListener() for each RadioButton? Or I have to do it for each RadioGroup? And where do I declare this statements: inside the for loop or outside? Or there is another way?

I'm very very lost here! Any kind of help would be apreciated! Thanks!

4

1 に答える 1

0

There are only one way to get it by using OnCheckedChangeListener You can create each listener and assign it to your RadioGroup in the loop but I am not recomment to do that. You can create multiple ID in your resource and the create just a single listener before loop and in the loop assign it to your RadioGroup ( Your Radiogroup need set the ID that create in the resource ). Here is example:

    OnCheckedChangeListener listener = new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (group.getId()) {
            case R.id.myradio_group1:
                // DO your work here
                break;

            default:
                break;
            }
        }
    };

    // your loop is here
    for(int i=0;i<numFilasFormulario;i++){
        ....
        RadioGroup rg = new RadioGroup(this);
        rg.setId(R.id.myradio_group1); // or simply just using rg.setId(i+1000);
        // make sure 1000, 1001, 1002, ... is already create at Resource
        ....
    }
于 2012-10-29T14:34:45.263 に答える