Skip to content Skip to sidebar Skip to footer

Dynamically Name Textview After Inflating Android

I m inflating 4 linear layouts multiple times in a scroll view. Each layout has a title in its header. The code snippet is given below. for(int j=0;j

Solution 1:

probably the TextView android: id in all layouts are the same, thus explaining the behavior of the method findViewById always returns the first occurrence

Solution 2:

Disclaimer: I feel like you are trying to work against the Android Framework, as opposed to working with it. My suggestion is a tad bit of an overhaul, but I do feel like it is the correct way to go about things.

I would recommend using a ListView, since you are repeating the same format many times. A listview will typically have a container of objects of objects, and an adapter responsible for mapping an object to a particular row within the listview. When you change the data in the underlying container, you will call the callback notifyDataSetChanged() for your adapter to notify it that you changed the underlying data and alert it to update your listview.

2 great resources/tutorials from the android developer site: ListView: http://developer.android.com/guide/topics/ui/layout/listview.html Adapters: http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews

Solution 3:

If i am right main_layout is a name view in your xml file.So You should use like this

 view1 = getLayoutInflater().inflate(R.layout.layout1, main_layout,false);
 TextViewtv= (TextView)main_layout.findViewById(R.id.tv1);

where tv is texview in layout1.xml

Post a Comment for "Dynamically Name Textview After Inflating Android"