添加的行一定数目Java中的一个表 [英] Adding a definite number of rows in a table in java

查看:170
本文介绍了添加的行一定数目Java中的一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建在Java表和行执行之前不知道的数目。所以,我有以下Java code这将创建该表。然而,当我尝试到表行添加到表的布局,它抛出非法状态异常。我将如何解决这个问题?

 地图<整型的TableRow> KMAP = NULL;
   的for(int i = 0; I< noofrows;我++)
   {         测试的TableRow =新的TableRow(本);
          KMAP =新的HashMap<整型的TableRow>();
         为(中间体K = 0; k 3;; k ++)
         {
           kMap.put(K,测试);
         }     }
TextView的空=新的TextView(本);//标题列/行
TextView的标题=新的TextView(本);
title.setText(Java的天气表);
TableRow.LayoutParams PARAMS =新TableRow.LayoutParams();
params.span = 6;rowTitle.addView(标题,则params);//一些更多的字段添加到行table1.addView(rowTitle);
table1.addView(kMap.get(0));
//抛出异常
table1.addView(kMap.get(1));
table1.addView(kMap.get(2));
//table1.addView(rowConditions);的setContentView(表1);

登录猫跟踪:

 七月9日至14日:02:50.265:E / AndroidRuntime(384):java.lang.IllegalStateException:产生的原因指定的小孩已经有一个父。您必须先对孩子的父母打电话removeView()。
7月9日至14日:02:50.265:E / AndroidRuntime(384):在android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
7月9日至14日:02:50.265:E / AndroidRuntime(384):在android.view.ViewGroup.addView(ViewGroup.java:1865)
7月9日至14日:02:50.265:E / AndroidRuntime(384):在android.widget.TableLayout.addView(TableLayout.java:418)
7月9日至14日:02:50.265:E / AndroidRuntime(384):在android.view.ViewGroup.addView(ViewGroup.java:1822)
7月9日至14日:02:50.265:E / AndroidRuntime(384):在android.widget.TableLayout.addView(TableLayout.java:400)
7月9日至14日:02:50.265:E / AndroidRuntime(384):在android.view.ViewGroup.addView(ViewGroup.java:1802)
7月9日至14日:02:50.265:E / AndroidRuntime(384):在android.widget.TableLayout.addView(TableLayout.java:391)


解决方案

这是不是与你的code的唯一问题,但你抱怨的原因是:你只创建了一个的TableRow和问题正在增加它的三倍。您的KMAP需要包含3个不同的TableRow对象,而不是同一对象的3倍。

I have to create a table in java and the number of rows are not known prior to execution. So i have the following java code which creates the table. However , when I try to add the table row to the table layout it throws an illegal state exception. How would I solve this issue?

 Map<Integer,TableRow> kMap = null;
   for(int i=0;i<noofrows;i++)
   {

         TableRow test=new TableRow(this);
          kMap = new HashMap<Integer,TableRow>();
         for(int k = 0; k < 3; k++)
         {
           kMap.put(k, test);
         }

     }
TextView empty = new TextView(this);

// title column/row
TextView title = new TextView(this);
title.setText("Java Weather Table");


TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 6;

rowTitle.addView(title, params);

//Add some more fields to the rows

table1.addView(rowTitle);
table1.addView(kMap.get(0));
//Exception is thrown
table1.addView(kMap.get(1));
table1.addView(kMap.get(2));
//table1.addView(rowConditions);

setContentView(table1);

Log cat trace:

09-14 07:02:50.265: E/AndroidRuntime(384): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-14 07:02:50.265: E/AndroidRuntime(384):  at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
09-14 07:02:50.265: E/AndroidRuntime(384):  at android.view.ViewGroup.addView(ViewGroup.java:1865)
09-14 07:02:50.265: E/AndroidRuntime(384):  at android.widget.TableLayout.addView(TableLayout.java:418)
09-14 07:02:50.265: E/AndroidRuntime(384):  at android.view.ViewGroup.addView(ViewGroup.java:1822)
09-14 07:02:50.265: E/AndroidRuntime(384):  at android.widget.TableLayout.addView(TableLayout.java:400)
09-14 07:02:50.265: E/AndroidRuntime(384):  at android.view.ViewGroup.addView(ViewGroup.java:1802)
09-14 07:02:50.265: E/AndroidRuntime(384):  at android.widget.TableLayout.addView(TableLayout.java:391)

解决方案

This isn't the only problem with your code, but the issue you're complaining about is caused because you only created one TableRow and are adding it three times. Your kMap needs to contain 3 different TableRow objects, not the same object 3 times.

这篇关于添加的行一定数目Java中的一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆