0

I'am는 배수 it.My 코드에서 레이아웃을 infalted 된 TableRow를 만들려고하기 위해 약 2 시간 동안 갇혀에 프로그래밍 방식으로 TableRow 할 수는 다음과 같습니다addView for 루프

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); 

오류 I을 잘못은 무엇을 I'am

The specified child already has a parent. You must call removeView() on the child's parent first.

을 나는 그것을 바로 방법을 수행해야 얻을?

답변

0

보기에는 한 개의 상위 항목 만있을 수 있습니다. 동일한 뷰를 반복해서 추가하려고합니다.

for(String item: items) { 

    tvCellName.setText(item); 

    tableRow.addView(tvCellName); // this will add the same view. 
} 

당신처럼, 그것을 팽창 당신이 for 내부 뷰를 동적으로 생성해야 다른보기를 추가하지하려면 : 귀하의 의견을 부풀려

tvCellName = new TextView(this);//this creates a new view, that doesn't have a parent. 

편집 내부 용 :

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); 
    } 
+0

문제는 내 TextVi ew는 부풀려진 레이아웃에서 나올 필요가 있습니다. 'TextView tvCellName = (TextView) headerCell.findViewById (R.id.tvCellName); ' – TGeorge

+0

보기를 동적으로 만들어야합니다. 너의 문제는 그렇지 않아. 문제는 뷰 **를 여러 번 추가 할 수 없다는 것입니다. –

+0

죄송합니다. 것은'tvCellName'을'tableRow'에 추가 할 필요가 없다는 것입니다. 테이블 행에'TextView'가있는'headerCell'을 추가해야합니다. – TGeorge