user3060230 user3060230 - 19 days ago 9
Android Question

Programatically adding rows to a table layout

I want to add rows to a table layout as per how many data stored and I have tried code given below but I am getting error saying child node parent already has child use removeVies I used it but still getting errors

someList = new ArrayList<TextView>();
SKUDatabase skdb = new SKUDatabase(SearchActivity.this);
skdb.open();
length = skdb.getRowLength();
if(length==0){
noDataStored();
}
skdb.close();
count = length*3;
addTL = (TableLayout) findViewById(R.id.tlSearchUse);
addTL.removeAllViews();
TableRow tableRow = new TableRow(getApplicationContext());
tableRow.setLayoutParams(new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
addTL.removeAllViews();
addTL.removeAllViewsInLayout();
addTL.removeView(tableRow);
for (int i = 0; i < length; i++) {
for(int j=0;j<3;j++){
if (j==0) {
TextView columsView = new TextView(getApplicationContext());
columsView.setLayoutParams(new TableRow.LayoutParams(436,
TableLayout.LayoutParams.WRAP_CONTENT));
columsView.setTextColor(color.Black);
columsView.setBackgroundResource(R.drawable.rt_col_even);
tableRow.addView(columsView);
someList.add(columsView);

} else {
TextView columsView = new TextView(getApplicationContext());
columsView.setLayoutParams(new TableRow.LayoutParams(111,
TableLayout.LayoutParams.MATCH_PARENT));
columsView.setTextColor(color.Black);
columsView.setBackgroundResource(R.drawable.rt_col_even);
tableRow.addView(columsView);
someList.add(columsView);

}
addTL.addView(tableRow);
}


}




}


My logCat is

12-05 09:28:08.139: E/AndroidRuntime(5552): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.grayroutes.skusearch/com.grayroutes.skusearch.SearchActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.app.ActivityThread.access$600(ActivityThread.java:140)
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.os.Looper.loop(Looper.java:137)
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.app.ActivityThread.main(ActivityThread.java:4898)
12-05 09:28:08.139: E/AndroidRuntime(5552): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 09:28:08.139: E/AndroidRuntime(5552): at java.lang.reflect.Method.invoke(Method.java:511)
12-05 09:28:08.139: E/AndroidRuntime(5552): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
12-05 09:28:08.139: E/AndroidRuntime(5552): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
12-05 09:28:08.139: E/AndroidRuntime(5552): at dalvik.system.NativeStart.main(Native Method)
12-05 09:28:08.139: E/AndroidRuntime(5552): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.view.ViewGroup.addViewInner(ViewGroup.java:3668)
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.view.ViewGroup.addView(ViewGroup.java:3539)
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.widget.TableLayout.addView(TableLayout.java:425)
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.view.ViewGroup.addView(ViewGroup.java:3484)
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.widget.TableLayout.addView(TableLayout.java:407)
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.view.ViewGroup.addView(ViewGroup.java:3460)
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.widget.TableLayout.addView(TableLayout.java:398)
12-05 09:28:08.139: E/AndroidRuntime(5552): at com.grayroutes.skusearch.SearchActivity.onCreate(SearchActivity.java:84)
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.app.Activity.performCreate(Activity.java:5206)
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-05 09:28:08.139: E/AndroidRuntime(5552): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)


Please help me;
I am new to android.

Answer

Create object of tableRow inside your for loop like below..

for (int i = 0; i < length; i++) {
            for(int j=0;j<3;j++){
                TableRow tableRow = new TableRow(getApplicationContext());
                tableRow.setLayoutParams(new TableLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            if (j==0) {
                TextView columsView = new TextView(getApplicationContext());
                columsView.setLayoutParams(new TableRow.LayoutParams(436,
                        TableLayout.LayoutParams.WRAP_CONTENT));
                columsView.setTextColor(color.Black);
                columsView.setBackgroundResource(R.drawable.rt_col_even);
                tableRow.addView(columsView);
                someList.add(columsView);

            } else {
                TextView columsView = new TextView(getApplicationContext());
                columsView.setLayoutParams(new TableRow.LayoutParams(111,
                        TableLayout.LayoutParams.MATCH_PARENT));
                columsView.setTextColor(color.Black);
                columsView.setBackgroundResource(R.drawable.rt_col_even);
                tableRow.addView(columsView);
                someList.add(columsView);

            }
            addTL.addView(tableRow);
            }

This will solve the problem...

Comments