Group Textviews and then access them

Asked

Viewed 166 times

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 Cada TextView in a different layout

  • I can not understand what the purpose. Can explain better why you want to group them?

  • @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...

1 answer

1


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

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 take back each of the TextView using the respective id:

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

Another possibility is to define the key ranging 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);
    ...
    ...
} 

We can alternatively get all the TextView in a Layout using ViewGroup.getChildAt() and keep 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);
        }
    }
}  

Taking the code of your comment would be 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 do not relate to each other, are not part of the same view.
If they do, just go to storeTextView() the highest layout on Tree. How recursiveness is used, all TextView are found.

  • Okay, I’m gonna try...

  • Ramaral, I did as you said: for (int i = 0; i < 7; i++) {v = Adapter.getLayout(i); map.put(i, (Linearlayout)v);} I was able to access the 7 layouts, but, unfortunately, access the Textviews in them did not become easier

  • from your code I got what I wanted. Maybe it’s not the ideal way, but I found it easier for me. Thanks a lot for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.