Group TextViews and then access them

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 can access them through the group, eg:

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 this? I tried ViewGroup , but I could not.

SOLUTION:
From the reply of @ramaral, my code looks 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
                    }
                }
            }
        }
    }

I now have access to TextView's of various layouts and their id's (a1, a2 ... a70) are in order.

    
asked by anonymous 13.01.2015 / 00:08

1 answer

1

You can use HashMap to save TextView , using the id key as the key:

Map<Integer,TextView> map = new HashMap<Integer, TextView>();
map.put(tv1.getId(), tv1);
map.put(tv2.getId(), tv2);
map.put(tv3.getId(), tv3);  

You can retrieve each of the TextView using its id :

TextView tv = map.get(R.id.tv1);

Another possibility is to set the key varying from zero to n and then use for to access each of the TextView

Map<Integer,TextView> map = new HashMap<Integer, TextView>();
map.put(0, tv1);
map.put(1, tv2);
map.put(2, tv3); 

for (int i = 0; i < map.size(); i++) {
    TextView tv = map.get(i);
    ...
    ...
} 

Alternatively, you can get all TextView in a Layout using ViewGroup.getChildAt() and save them in HashMap

private void storeTextView(ViewGroup viewGroup) {

    int count = viewGroup.getChildCount();
    for (int i = 0; i < count; i++) {
        View view = viewGroup.getChildAt(i);

        if (view instanceof ViewGroup)
            storeTextView((ViewGroup) view);//Recursivamente percorre toda a Tree
        else if (view instanceof TextView) {
            TextView textView = (TextView) view;
            map.put(textView.getId(), textView);
        }
    }
}  

Picking up your comment code would look like this:

Map<Integer,TextView> map = new HashMap<Integer, TextView>();
for (int i = 0; i < 7; i++) {
    v = adapter.getLayout(i);
    storeTextView((ViewGroup)v);
}

I assume that the 7 layouts are not related to each other, they are not part of the same view.
If they do, just move to storeTextView() the highest layout in the tree. Since recursion is used, all TextView are found.

    
13.01.2015 / 01:47