Android - find an item by ID

Well, I have a very simple and yet complicated question.

I have 10 flags, all are called 'check', each has a unique identifier from 1 to 10. When I click a button in my application, I want to know which flags are checked and which are not.

I am sure the answer is very simple, but I can’t figure out how I can do this. I have a code for the button, I can’t decide how to check each flag by identifier, when they are all called “check”. I hope one of you guys can help me.

+5
source share
3 answers
CheckBox chkbx1 = (CheckBox) findViewById(R.id.***);
chbkx1.isChecked();

isChecked () returns true or false, depending on the checked flag state. hope this helps!

+9

,

OnClickListener

boolean checkedState[] = new boolean[10];

for(int i=0; i <= 10; i++) 
{
    CheckBox c = (CheckBox)findViewById(i);
    checkedState[i] = c.isChecked();
}

, .

+7

Name the flags differently from each other, then use their identifiers. If you use eclipse, this should be very simple. Just open the xml file and change the name there or use a visual element and click on this checkbox and then change the properties. Then just check the boxes by their identifier.

As soon as you change names, Brandon provides a great example of how to use an identifier and check if they are checked.

+1
source

All Articles