用不同的数据膨胀几个相同的布局



如何用不同的文本和图像膨胀几个相同的布局?我尝试使用此代码

    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 和适配器,但我想了解是否可以在没有它们的情况下完成。

我假设ImageView和TextView - tourImagetourText - 是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);

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

相关内容

最新更新