android:使用充气机仅列表的第一项被正确填充

发布于 2024-11-26 15:58:21 字数 1176 浏览 1 评论 0原文

我有一个布局要在每个类别的另一个布局中动态地膨胀:

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):

<?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>

但这样只有第一个项目会填充正确的值。其他的categoryList显示为xxx(如xml中所述)。

如何显示列表中所有正确的名称?

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

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>

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玉环 2024-12-03 15:58:21

我最近两次遇到同样的问题。我第一次使用 LinearLayout 所以第一个答案效果很好 - 将每个视图 id 设置为 0 不是问题。

然而,我第二次使用RelativeLayout - 将ids设置为0只会破坏整个视图,因为它被定位为“leftTo:some_id”等等。

我尝试了一点,结果发现,在设置所有组件后将视图添加到其容器中可以解决问题(我们只需要使用防止将膨胀布局添加到其父级的参数对其进行膨胀)。这个想法是让膨胀的视图位于任何其他视图之外,包含具有相同 id 的组件,因此 findViewById 只能找到我们感兴趣的视图,而不是之前膨胀的视图。

ViewGroup inflatedLayout = (ViewGroup) layoutInflater.inflate(R.layout.some_id, mDirectChildInScrollView, false);
ImageView imageView = (ImageView) inflatedLayout.findViewById(R.id.image_view);
... do your stuff here ...
mDirectChildInScrollView.addView(inflatedLayout);

您可以使用 ViewHolder 模式稍后访问所需的元素,而无需调用 findViewById。

I ran into the same problem recently twice. First time I was using LinearLayout so the first answer worked out just fine - setting every view id to 0 was not an issue.

However the second time I was using RelativeLayout - setting ids to 0 just broke whole view, since it is positioned as "leftTo:some_id" and so on.

I tried a little and it turned out that adding the view to its container AFTER setting its all components solves the problem (we just need to inflate it with parameter that prevents adding inflated layout to its parent). The idea is to have the inflated view outside any other view, that contains components with the same id, so findViewById can find only the one we are interested in and not the ones inflated previously.

ViewGroup inflatedLayout = (ViewGroup) layoutInflater.inflate(R.layout.some_id, mDirectChildInScrollView, false);
ImageView imageView = (ImageView) inflatedLayout.findViewById(R.id.image_view);
... do your stuff here ...
mDirectChildInScrollView.addView(inflatedLayout);

You can use ViewHolder pattern to access desired element later wihtout calling findViewById.

原来分手还会想你 2024-12-03 15:58:21

问题是 linLayout 将指向您的主布局容器 l。这样您只能找到 id 为 R.id.label 的视图的第一个实例。

一种简单的解决方案是在初始化标签后更改标签的 id。像这样的东西:

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.
    }

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.

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.
    }
随波逐流 2024-12-03 15:58:21

在我看来,您应该在循环中动态创建新的 TextView,而不是重复尝试复制和编辑同一视图。从逻辑上来说,这似乎是一个问题。我将创建一个类别列表大小的文本视图数组,并使用它来存储对您在循环中创建的新 TextView 的引用:

TextView textViewArray[] = new TextView[categoryList.length];

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

int i = 0; //Used for array addressing

    for (CategoryEB e :categoryList){
        TextView view = new TextView(); //declare new TextView
        view.setText(e.getName()); //set Text
        textViewArray[i] = view; //Save into array for future reference
        linLayout.addView(view); //Add TextView to linearLayout
        i++;
    }

It seems to me you should be dynamically creating new TextViews in your loop rather than repeadetly trying to duplicate and edit the same view. Logically, this seems to be an issue. I would create an array of textviews the size of categoryList and use it to store references to new TextViews you create in your loop:

TextView textViewArray[] = new TextView[categoryList.length];

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

int i = 0; //Used for array addressing

    for (CategoryEB e :categoryList){
        TextView view = new TextView(); //declare new TextView
        view.setText(e.getName()); //set Text
        textViewArray[i] = view; //Save into array for future reference
        linLayout.addView(view); //Add TextView to linearLayout
        i++;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文