机器人:使用吹气仅列表的第一项获得正确填充 [英] android: using inflater only first item of the list gets correctly populated

查看:219
本文介绍了机器人:使用吹气仅列表的第一项获得正确填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局,以另一种布局dinamically每个类别的膨胀:

I have a layout to be inflated in another layout dinamically for each category:

LinearLayout l = (LinearLayout) findViewById(R.id.container);

        for (CategoryEB e :categoryList){
            LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LinearLayout linLayout = (LinearLayout)layoutInflater.inflate(R.layout.inflated_layout, l);
            TextView view = (TextView)linLayout.findViewById(R.id.lable);
            view.setText(e.getName());
        }

这是被夸大了XML(inflated_layout.xml):

this is the xml to be inflated (inflated_layout.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView android:id="@+id/lable"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="xxx"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:paddingLeft="6dip"/>

</LinearLayout>

,但这种方式只在第一个项得到正确的值填充。的所属分类的其他的被示出为XXX(如在XML描述)。

but in this way only the first item gets populated with the correct value. The other ones of the categoryList are shown as xxx (as described in the xml).

我怎样才能在列表中显示所有正确的名称?

How can I display all the correct names in the list?

推荐答案

的问题是,linLayout将指向你的主要布局容器,L。所以,你只能找到ID为R.id.label视图的第一个实例。

The problem is that linLayout will point to your main layout container, l. So that you will only find the first instance of a view with id R.id.label.

一个简单的解决办法是改变标签的ID一旦你初始化它。事情是这样的:

One simple solution would be to change the id of the label once you've initialized it. Something like this:

LinearLayout l = (LinearLayout) findViewById(R.id.container);

    for (CategoryEB e :categoryList){
        LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout linLayout = (LinearLayout)layoutInflater.inflate(R.layout.inflated_layout, l);
        TextView view = (TextView)linLayout.findViewById(R.id.lable);
        view.setText(e.getName());
        view.setId(0);  //this way you wont find the same view twice.
    }

这篇关于机器人:使用吹气仅列表的第一项获得正确填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆