1

iterating through a View's components the following code works:

if (child.getClass() == EditText.class) {
  ...
} else if (child.getClass() == TextView.class) {
  ...

but this doesn't:

} else if (child.getClass() == Spinner.class) {
  ...

What's the difference between the Spinner class an the other two ?


My mistake ... I was previousy checking if it was a ViewGroup object so It never reached the condition

Thanks

xain
  • 13,159
  • 17
  • 75
  • 119

3 Answers3

2

My mistake ... I was previousy checking if it was a ViewGroup object so It never reached the condition

xain
  • 13,159
  • 17
  • 75
  • 119
1
 if(child.getClass() instanceof Spinner.class){
 ...

edit:

I found Stackoverflow question that explain it:

Any reason to prefer getClass() over instanceof when generating .equals()?

Community
  • 1
  • 1
pawelzieba
  • 16,082
  • 3
  • 46
  • 72
1

Have you considered using

if(child instanceof EditText){}
else if(child instanceof TextView){}
else if(child instanceof Spinner){}
runor49
  • 3,870
  • 1
  • 21
  • 18