Reputation: 1303
I'am stuck for about 2 hour in order to try to create a TableRow who was multiples infalted layouts in it.My code looks like this:
LayoutInflater inflater = LayoutInflater.from(this);
TableLayout tblLayout = inflater.inflate(R.layout.tblL,parent,false);
TableRow tableRow = (TableRow) tblListItems.findViewById(R.id.tableRow );
View headerCell = inflater.inflate(R.layout.header_col,tblListItems,false);
TextView tvCellName = (TextView) headerCell.findViewById(R.id.tvCellName);
for(String item: items) {
tvCellName.setText(item);
tableRow.addView(tvCellName);
}
relativeLayout2.addView(tblLayout);
The error I get :
The specified child already has a parent. You must call removeView() on the child's parent first.
What I'am doing wrong and how should I do it right ?
Upvotes: 0
Views: 1269
Reputation: 20500
A view can only have one parent. You're trying to add the same view over and over again.
for(String item: items) {
tvCellName.setText(item);
tableRow.addView(tvCellName); // this will add the same view.
}
If you want to add another view you need to create the view dynamically, inside the for
, and not inflate it, like:
tvCellName = new TextView(this);//this creates a new view, that doesn't have a parent.
edit
Inflate your views inside the for:
View headerCell;
TextView tvCellName;
for(String item: items) {
headerCell = inflater.inflate(R.layout.header_col,tblListItems,false);
tvCellName = (TextView) headerCell.findViewById(R.id.tvCellName);
tvCellName.setText(item);
tableRow.addView(tvCellName);
}
Upvotes: 1