Android: with the help of an inflatable device, only the first element of the list is correctly filled

I have a layout that needs to be inflated in a different location dynamically 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 for pumping (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 element is filled with the correct value. Others from the list of categories are shown as xxx (as described in xml).

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

0
source share
3 answers

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

One simple solution would be to change the tag ID after it is initialized. Something like that:

 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. } 
+1
source

I recently ran into the same problem. The first time I used LinearLayout, so the first answer was just good - setting each view identifier to 0 was not a problem.

However, the second time I used RelativeLayout - set id to 0, the whole view just broke, since it is positioned as "leftTo: some_id", etc.

I tried a little, and it turned out that adding a view to its container AFTER installing all its components solves the problem (we just need to inflate it with a parameter that prevents adding a bloated layout to its parent element). The idea is to have an overestimated view outside any other view containing components with the same identifier, so findViewById can only find one that interests us, and not the one that was pumped up earlier.

 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 the ViewHolder template to access the desired item later without calling findViewById.

+2
source

It seems to me that you should dynamically create new TextViews in your loop, and not try to duplicate and edit the same view. Logically, this seems like a problem. I would create an array of textviews of the size categoryList and use it to store links to new TextViews created 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++; } 
0
source

All Articles