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