android:使用充气机仅列表的第一项被正确填充
我有一个布局要在每个类别的另一个布局中动态地膨胀:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近两次遇到同样的问题。我第一次使用 LinearLayout 所以第一个答案效果很好 - 将每个视图 id 设置为 0 不是问题。
然而,我第二次使用RelativeLayout - 将ids设置为0只会破坏整个视图,因为它被定位为“leftTo:some_id”等等。
我尝试了一点,结果发现,在设置所有组件后将视图添加到其容器中可以解决问题(我们只需要使用防止将膨胀布局添加到其父级的参数对其进行膨胀)。这个想法是让膨胀的视图位于任何其他视图之外,包含具有相同 id 的组件,因此 findViewById 只能找到我们感兴趣的视图,而不是之前膨胀的视图。
您可以使用 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.
You can use ViewHolder pattern to access desired element later wihtout calling findViewById.
问题是 linLayout 将指向您的主布局容器 l。这样您只能找到 id 为 R.id.label 的视图的第一个实例。
一种简单的解决方案是在初始化标签后更改标签的 id。像这样的东西:
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:
在我看来,您应该在循环中动态创建新的 TextView,而不是重复尝试复制和编辑同一视图。从逻辑上来说,这似乎是一个问题。我将创建一个类别列表大小的文本视图数组,并使用它来存储对您在循环中创建的新 TextView 的引用:
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: