0
public void changeCurrency(RelativeLayout layout) {
    for (int i = 0; i < layout.getChildCount(); i++) {
        View v = layout.getChildAt(i);
        Class c = v.getClass();
        if (c == EditText.class) {
            // validate EditClass
        } else if (c == TextView.class) {
            //validate RadioButton
        } 
    }
}

In the above code I'm trying to iterate through gui elements in a layout and validate their contents. I'm struggling at the commented parts.

I.e. getting access to the EditText's text value..

I can't figure out how to cast the c object to a EditText to check the values.

Ideas?

Chris G
  • 449
  • 1
  • 5
  • 19

2 Answers2

4

Try using the logic below

View v = layout.getChildAt(i);

if (v instanceof EditText) {
    EditText et = (EditText) v;

    //Do stuff

} else if (v instanceof TextView) {

    //Do other stuff

}

Since EditText is a subclass of TextView, you need to check for EditText first. An EditText will test positive as an instance of TextView.

Ahmad
  • 69,608
  • 17
  • 111
  • 137
NameSpace
  • 10,009
  • 3
  • 39
  • 40
  • Thanks.. realised this myself after I kept trying to cast C instead of V for some reason.. It's late.. lol. Thanks again. – Chris G Feb 09 '14 at 03:15
1

Most of the views that use text extend from TextView. This should be sufficient if all you are doing is validating text.

public void changeCurrency(RelativeLayout layout) {
    for (int i = 0; i < layout.getChildCount(); i++) {
        View v = layout.getChildAt(i);
        if (v instanceof TextView) {
            TextView t = (TextView) v;
            String text = t.getText().toString();
            // ...
        }
    }
}
Karakuri
  • 38,365
  • 12
  • 84
  • 104