1
Example:
I have 3 TextViews
and its id's
:
TextView tv1 -> id: "um"
TextView tv2 -> id: "dois"
TextView tv3 -> id: "tres"
I would like to group them in some way that I can access them through the group, ex.:
ViewGroup vg;
vg.addView(tv1);
vg.addView(tv2);
vg.addView(tv3);
TextView tv11 = (TextView) vg.findViewById("um");
TextView tv22 = (TextView) vg.findViewById("dois");
TextView tv33 = (TextView) vg.findViewById("tres");
Is it possible to do that? I tried to ViewGroup
, but I couldn’t.
SOLUTION:
From the @ramaral response, my code looked like this:
for (int i = 0; i < 7; i++) {
ViewGroup vg = (ViewGroup) adapter.getViewAtPosition(i); //pega os layouts
for (int j = 0; j < vg.getChildCount(); j++) { //percorre seus elementos
View v = vg.getChildAt(j);
if (v instanceof ViewGroup) { //pega tudo que for ViewGroup, inclusive outros layouts
for (int h = 0; h < ((ViewGroup) v).getChildCount(); h++) { //percorre novamente
View v2 = ((ViewGroup) v).getChildAt(h);
if (v2 instanceof TextView) { //pega tudo que for TextView
map.put(v2.getId(), (TextView) v2); //adiciona as TextViews no HashMap
}
}
}
}
}
Now I have access to TextView's
of several layouts and their id's
(a1, a2 ... a70)
are in order.
Where were these created
TextView
?– ramaral
@ramaral Cada
TextView
in a different layout– Felipe
I can not understand what the purpose. Can explain better why you want to group them?
– ramaral
@ramaral I have 7 layouts, each with about 10 Textviews. The id’s of them are in sequence (id’s: a1, a2, ... A70). If I group them together, access all in an easier way with a for, like this...
– Felipe