我在Android上创建我的第一个应用程序,因此我有低开发android应用程序的经验,而且我也是第一次使用java。在Android Studio中动态生成并显示表格
我想要什么? 我想从ArrayList生成表格
什么问题? 我生成表时有问题。当“for”循环完成的调试器站在catch块中时(但是当我将鼠标移到“e”上时,它会写入 - “局部变量e未定义”,我看不到某个错误)之后,我在电话屏幕上看到 - “不幸的是,应用程序已关闭“
没有For Loop会发生什么? 无For循环一切正常,当我试图只显示一个对象。
请帮忙给我的建议,感谢名单。
野兔是Java代码:
try {
ArrayList otherPersons = new ArrayList();
otherPerosons = GetPersonsList();//from service
// /* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout) findViewById(R.id.dynamictable);
TextView[] title = new TextView[1000];
TableRow[] tr = new TableRow[1000];
LinearLayout.LayoutParams trparams;
LinearLayout.LayoutParams titleparams;
for (int y=0; y < otherPersons.size(); y++) {
/* Create a new row to be added. */
tr[y] = new TableRow(this);
tr[y].setId(y+100);
tr[y].setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
trparams = (LinearLayout.LayoutParams) tr[y].getLayoutParams();
trparams.setMargins(0, 8, 0, 5); //substitute parameters for left, top, right, bottom
tr[y].setLayoutParams(trparams);
/* Create a TextView to be the row-content. */
title[y] = new TextView(this);
title[y].setText(otherPersons.get(y).Name);
title[y].setId(y +200);
title[y].setTextColor(Color.parseColor("#000000"));
title[y].setGravity(Gravity.LEFT);
title[y].setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 34));
titleparams = (LinearLayout.LayoutParams) title[y].getLayoutParams();
titleparams.setMargins(5, 0, 0, 0); //substitute parameters for left, top, right, bottom
title[y].setLayoutParams(titleparams);
tr[y].addView(title[y]);
/* Add row to TableLayout. */
tl.addView(tr[y], new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
} catch (Exception e) {
e.printStackTrace();
}
野兔是我设计的XML:
android:id="@+id/dynamictable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="39dp"
android:stretchColumns="*"
android:background="#ffB224">