too much checkbox, how can i do?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

too much checkbox, how can i do?

Postby slashgill » Mon Jan 25, 2010 5:25 am

Hi, there are too much CheckBox in my program, like this
CheckBox checkbox01 = (CheckBox) findViewById(R.id.CheckBox01);
CheckBox checkbox02 = (CheckBox) findViewById(R.id.CheckBox02);
CheckBox checkbox03 = (CheckBox) findViewById(R.id.CheckBox03);
CheckBox checkbox04 = (CheckBox) findViewById(R.id.CheckBox04);
CheckBox checkbox05 = (CheckBox) findViewById(R.id.CheckBox05);
CheckBox checkbox06 = (CheckBox) findViewById(R.id.CheckBox06);
CheckBox checkbox07 = (CheckBox) findViewById(R.id.CheckBox07);
CheckBox checkbox08 = (CheckBox) findViewById(R.id.CheckBox08);
CheckBox checkbox09 = (CheckBox) findViewById(R.id.CheckBox09);
CheckBox checkbox10 = (CheckBox) findViewById(R.id.CheckBox10);

it is not a good idea, i try to change like that:
CheckBox checkbox[]=new CheckBox[10];
for(int i=0;i<10;i++){
checkbox[i] = (CheckBox) findViewById(R.id.CheckBox01+i);
}

When i run this program, i got an error message
The application XX(process com.xx.xxx)has stopped unexpectedly. Please try again.

how could i solve it?
thanks. :?
slashgill
Junior Developer
Junior Developer
 
Posts: 16
Joined: Mon Jan 25, 2010 5:14 am

Top

Postby spring » Mon Jan 25, 2010 6:44 am

R.id.Checkbox01 ..... R.id.CheckBox10

perhaps,they are hex number and not consecutive.

you can use an array to hold the id of controls like that

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. int[] ids = {R.id.CheckBox01, .... , R.id.CheckBox10};
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


you can access them by

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. (CheckBox)findViewById(ids[i]);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
spring
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Jan 09, 2010 4:37 am
Location: China

Postby slashgill » Thu Jan 28, 2010 2:51 pm

thanks, i got it!
slashgill
Junior Developer
Junior Developer
 
Posts: 16
Joined: Mon Jan 25, 2010 5:14 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 5 guests