Domanda

I am looping through a list of checkboxes upon click of a button. What I am looking to do is grab the name of the checkbox at runtime to strip out an integer value specified within the name. I am not looking to get the value nor the id. So in the strings.xml file, <string name="checkList1">Pain assessment.</string>, I am trying to get checkList1 at run time. I can get the text without a problem. Currently I am looping through the view elements with the code below:

RelativeLayout root = (RelativeLayout)findViewById(R.id.Root);

            for (int i = 0; i < root.getChildCount(); i++)
            {
                View v1 = root.getChildAt(i);
                Class c = v1.getClass();

                if (c == CheckBox.class)
                {
                    CheckBox thisBox = (CheckBox)v1;

                    if (thisBox.isChecked())
                    {
                        String text = (String)thisBox.ge;

                        DoDailyCheckListUpdate(thisBox.isChecked(),checkBoxCount);
                        countItemsFinished++;
                    }

                    checkBoxCount++;
                }
            }

What I am looking for is to somehow get the name of Checkbox thisBox. So when it loops through and hits the Pain Assessment checkbox, I want to be able to pull out checkList1. Without going as far as ripping through the strings.xml file based on the text I find to get the name, I was hoping maybe there was a simpler solution that I maybe overlooking.

Thank You in advance.

È stato utile?

Soluzione

CheckBox extends from TextView so to retrieve a text from it is quite simple :

String text = thisBox.getText().toString();

http://developer.android.com/reference/android/widget/CheckBox.html

If you want to retrieve the key name of the string. I suggest you put it into the tag of the object :

thisBox.setTag(getResources().getResourceEntryName(R.string. checkList1);

Retrieve it like that :

String text = (String)thisBox.getTag();

that should do the trick.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top