0

如何使用不同的文本和图像为多个相同的布局充气?我尝试使用此代码

    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);
    }

但是在第一个膨胀布局中设置了文本和图像,替换了以前的文本和图像。我明白会发生什么,因为布局具有相同的 ID。我可以解决吗?我知道 ListView 和适配器,但我想了解是否可以在没有它们的情况下完成。

4

1 回答 1

2

我假设 ImageView 和 TextView - tourImage, tourText- 是R.layout.tour_item_for_guide_info.

如果是这样,那么当您引用它时,您应该使用child视图来获取它们。

换句话说,而不是:

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

你应该有:

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

不确定这是否会明确解决您的问题,但它看起来像一个错误。

于 2012-09-18T14:42:44.957 回答