0

How can I inflate several same layouts with diffrent texts and images? I tried use this code

    for (final Tour t : info.tours) {
        final LinearLayout list = (LinearLayout) findViewById(R.id.tours_list);
        Log.d(Const.LOG_TAG, "Add child view to tours_list");
        final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View child = inflater.inflate(R.layout.tour_item_for_guide_info,
                list, true);
        final ImageView tourImage = (ImageView) findViewById(R.id.tour_image);
        final String tourImgUrl = Const.HOST + t.image;
        imageLoader.DisplayImage(tourImgUrl, tourImage);
        TextView tourText = (TextView) findViewById(R.id.text);
        tourText.setText(t.name);
    }

but text and image setted in first inflated layout, replacing the previous text and image. I understand what it's happens because layouts have same ids. Can I resolve it? I know about ListView and adapters, but I would like to learn if it might be done without them.

Yara_M
  • 633
  • 9
  • 29

1 Answers1

2

I assume that the ImageView and TextView - tourImage, tourText - are elements of R.layout.tour_item_for_guide_info.

If this is so, then when you reference it, you should use the child view to get them.

With other words, instead of:

ImageView tourImage = (ImageView) findViewById(R.id.tour_image);
TextView tourText = (TextView) findViewById(R.id.text);

You should have:

ImageView tourImage = (ImageView)child.findViewById(R.id.tour_image);
TextView tourText = (TextView)child.findViewById(R.id.text);

Not sure if this will definetly fix your problem, but it looks like a bug.

Andy Res
  • 15,963
  • 5
  • 60
  • 96