1

I am adding views to a layout at run time. thee views are like so:

<LinearLayout android:id="@+id/ll1">
<LinearLayout android:id="@+id/ll2" >
    <ImageView android:id="@+id/iv1" />
    <TextView android:id="@+id/tv1"/>
    <ImageView android:id="@+id/iv2"/>
</LinearLayout>

(I took a lot out to make it simple)

Now in code I am adding any number of these to a layout. How then do I access the ImageViews and TextViews in each addition of this view?

Currently I have this complicated way going through each child and doing a

if (View instanceof ImageView)

if I were to call parentView.getChildAt(0) (where parent view is ll2) would this always return iv1? and at "1" would this always return tv1?

Then I have been storing these objects in a vector of itself, to call on them later.

Is there an easier way of doing this? Like when I first add the whole thing, is there a way I can just set the sources of the image views, and set the text of the textviews without having to go and find them this way?

WIllJBD
  • 6,144
  • 3
  • 34
  • 44

1 Answers1

2

When you inflated your view,find it's children and set a specific Tag for each child(use setTag() ).When you need to recognize,use getTag().You can find good idea in about usage of getTag and setTag in What is the main purpose of setTag() getTag() methods of View? and Android:setTag() and getTag() usage for changing the controls..

Community
  • 1
  • 1
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167