I have a View object and I try to set it. However, it always remains null
outside the function . Here I try to set it while being passed as a parameter, I also tried with return
-statement but to no avail. Most probably, because the problem is somewhere else and I just can't see it. Strangely, enough it gets set inside the function, but outside remains null. Code:
private EditText mView = null;
public void findViewByTag(ViewGroup vg, Object obj, View v) {
if (vg == null)
return;
for (int i = 0; i < vg.getChildCount(); i++) {
if (vg.getChildAt(i).getTag() != null) {
if (vg.getChildAt(i).getTag().toString().equals(obj)) {
Log.d("Found", "Yes." + obj.toString());//here it does get found
v = vg.getChildAt(i);
//tested, it is not null HERE
return;
}
}
}
for (int i = 0; i < vg.getChildCount(); i++) {
if (vg.getChildAt(i) instanceof ViewGroup) {
findViewByTag((ViewGroup) vg.getChildAt(i), obj, v);
}
}
}
Then I call it like findViewbyTag(mViewGroup, "_MyTag", mView)
. Here I find out that mView == null
.
As a side question, how can I stop the recursion from iterating all my views after having found the one? Is there a way to stop it iterating the other view branches? Probably not, but still.
EDIT: This also doesn't work out as said in the beginning above.
public View findViewByTag(ViewGroup vg, Object obj) {
if (vg == null)
return null;
for (int i = 0; i < vg.getChildCount(); i++) {
//because some are not set and we don't like NullPtrs
if (vg.getChildAt(i).getTag() != null) {
if (vg.getChildAt(i).getTag().toString().equals(obj)) {
Log.d("Found", "Yes." + obj.toString());
return vg.getChildAt(i);
}
}
}
for (int i = 0; i < vg.getChildCount(); i++) {
if (vg.getChildAt(i) instanceof ViewGroup) {
findViewByTag((ViewGroup) vg.getChildAt(i), obj);
}
}
return null;
}