I am mking an android app in which i am making dynamically table rows and then adding imageviews and textviews to it dynamically.
It adds the first row and then give the following exception
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I am using the following code for it:
ar1= new JSONArray(children.toString());
for(int mn=0;mn<ar1.length();mn++){
value=ar1.getString(mn);
TableRow tr = new TableRow(HomePageWithPhoneIsOfflineDialog.this);
tr.setBackgroundColor(Color.WHITE);
tr.setId(100+mn);
tr.setMinimumHeight(60);
tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView child= new TextView(HomePageWithPhoneIsOfflineDialog.this);
child.setId(200+mn);
child.setHeight(50);
child.setGravity(Gravity.CENTER);
child.setTextColor(Color.BLACK);
System.out.println(value);
child.setText(value);
System.out.println("adding iv");
tr.addView(iv,0);
System.out.println("adding child");
tr.addView(child);
System.out.println("adding table row");
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
}
Can anyone tell me how to solve this problem.
THAnks
The iv variable that you are adding, is not been created inside the loop so I imagine is created before… but in the second loop it’s added to two different Table Rows.