以编程方式将行添加到表布局

时间:2013-12-05 03:59:20

标签: android android-layout android-tablelayout

我想根据存储的数据量添加到表布局中的行,并且我尝试了下面给出的代码,但是我收到错误,说明子节点父节点已经有子节点使用removeVies我使用它但仍然出错

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


            }




    }

我的logCat是

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)

请帮帮我; 我是android的新手。

2 个答案:

答案 0 :(得分:0)

在你的for循环中创建tableRow的对象,如下所示..

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

这将解决问题...

答案 1 :(得分:0)

请参阅以下内容.. http://uniqueandroidtutorials.blogspot.in/2013/02/creating-dynamic-row-in-tablelayout.html

希望它可以帮到你很多。