Reputation: 8321
Following is the code here I was testing that when I inflate any particular widget then I cannot really do setText on it. it overwrites the text and returns the last string. Here in the output screen I can see five textview added from xml but the one programmatically I am trying to set is not happening I see only one text from str[] i.e str[5] which is the last one in the array. Please let me know if i am able to explain my problem.
public class TestInflate extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View[] inflated = new View[5];
for(int i = 0; i<5; i++)
{
TableLayout myTableLayout = (TableLayout)findViewById(R.id.TableLayout);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflated[i] = inflater.inflate(R.layout.testbutton, myTableLayout);
TextView userName = (TextView)inflated[i].findViewById(R.id.myName);
userName.setText(str[i]);//here i should get name but not getting
}
}
String[] str = {"a","s","d","r","t"};
}
and my testButton.xml is as follows
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TableRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<TextView
android:id="@+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:text="Name :"
android:textStyle="bold"
android:layout_weight=".25"
></TextView>
<TextView
android:id="@+id/myName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic"
android:layout_weight=".5"
></TextView>
</TableRow>
and main.xml is
<ScrollView
android:id="@+id/Scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout
android:id="@+id/TableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true">
</TableLayout>
</ScrollView>
Edit- and teh optput is like this
My output is like this
Name : t //str[5]
Name :
Name :
Name :
Name :
Upvotes: 4
Views: 4953
Reputation: 2819
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
container.addView(addView);
YOu can also try this inside your for loop. For Reference : Look Here
Upvotes: 0
Reputation: 37729
Hi this time it is tested. try this.
public class MainActivity extends Activity {
/** Called when the activity is first created. */
String[] str = {"a","s","d","r","t"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout myTableLayout = (TableLayout)findViewById(R.id.TableLayout);
LayoutInflatedView myClasses [] = new LayoutInflatedView[5];
for(int i = 0; i<5; i++)
{
myClasses[i] = new LayoutInflatedView(this, myTableLayout);
myTableLayout.addView(myClasses[i]);
myClasses[i].setUserName(str[i]);
}
}
}
where your LayoutInflatedView is:
public class LayoutInflatedView extends LinearLayout
{
TextView text;
public LayoutInflatedView(Context context, ViewGroup table)
{
super(context);
LayoutInflater layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.test_button,this);
text = (TextView)findViewById(R.id.myName);
}
public void setUserName(String text)
{
this.text.setText(text);
}
}
Check this and let us know if any problem occures.
Upvotes: 1