1
\$\begingroup\$

I have 18 RadioGroup. I am declaring the RadioGroup like this way

private RadioGroup kn1RadioGroup, kn2RadioGroup, kn4aRadioGroup, kn4bRadioGroup, kn4cRadioGroup, kn4dRadioGroup, kn4eRadioGroup, kn4fRadioGroup, kn4gRadioGroup, kn4hRadioGroup, kn4iRadioGroup,kn4jRadioGroup,ap2aRadioGroup,ap2bRadioGroup, ap2cRadioGroup,ap2dRadioGroup,ap2eRadioGroup,pr1RadioGroup; 

I have created getter and setter for these RadioGroup. And I am setting the RadioGroup id like this way

fragment.setKn1RadioGroup((RadioGroup) findViewById(R.id.kn1_radio_group)); fragment.setKn2RadioGroup((RadioGroup) findViewById(R.id.kn2_radio_group)); fragment.setPr1RadioGroup((RadioGroup) findViewById(R.id.pr1_radio_group)); fragment.setKn4aRadioGroup((RadioGroup) findViewById(R.id.kn4a_radio_group)); fragment.setKn4bRadioGroup((RadioGroup) findViewById(R.id.kn4b_radio_group)); fragment.setKn4cRadioGroup((RadioGroup) findViewById(R.id.kn4c_radio_group)); fragment.setKn4dRadioGroup((RadioGroup) findViewById(R.id.kn4d_radio_group)); fragment.setKn4eRadioGroup((RadioGroup) findViewById(R.id.kn4e_radio_group)); fragment.setKn4fRadioGroup((RadioGroup) findViewById(R.id.kn4f_radio_group)); fragment.setKn4gRadioGroup((RadioGroup) findViewById(R.id.kn4g_radio_group)); fragment.setKn4hRadioGroup((RadioGroup) findViewById(R.id.kn4h_radio_group)); fragment.setKn4iRadioGroup((RadioGroup) findViewById(R.id.kn4i_radio_group)); fragment.setKn4jRadioGroup((RadioGroup) findViewById(R.id.kn4j_radio_group)); fragment.setAp2aRadioGroup((RadioGroup) findViewById(R.id.ap2a_radio_group)); fragment.setAp2bRadioGroup((RadioGroup) findViewById(R.id.ap2b_radio_group)); fragment.setAp2cRadioGroup((RadioGroup) findViewById(R.id.ap2c_radio_group)); fragment.setAp2dRadioGroup((RadioGroup) findViewById(R.id.ap2d_radio_group)); fragment.setAp2eRadioGroup((RadioGroup) findViewById(R.id.ap2e_radio_group)); 

Is there any easy way to do it?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ So...can you explain why this question is different to your previous one? \$\endgroup\$ Commented Jan 22, 2014 at 11:15

1 Answer 1

2
\$\begingroup\$

The easiest way would be to define a constant integer array holding the ids:

private static final int[] ids = { R.id.kn1_radio_group, R.id.kn2_radio_group, R.id.pr1_radio_group, /* and so on... */}; 

And then using the same method as in my answer to your previous question.

However, your naming of these RadioGroups tells me that there might be another structure here. And I also suspect that there is more to your application than what we see here. So what you really should do is to think about the structure of your variables; Should a kn be a class? Should kn4 be an array on it's own, or should perhaps kn be a two-dimensional array (one dimension for the number and one for the letter. These are questions that none of us currently can answer considering the lack of context we have about your application. If you would explain a bit more about What your application is for and what a kn / ap RadioGroup is, then we might help you more. You probably have some reasons for naming the radio groups the way that you do, in which case a different structure than an array might be more useful.

Also, I would like to add something: Why not let the fragment take care of it's own initialization of the RadioGroups? Is perhaps the RadioGroups a part of the fragment's view already? If so, then there is no need to use all these setter methods for it.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.