动态添加GridView

时间:2013-01-30 11:17:53

标签: android android-widget

我想以表格形式显示数据。但问题是活动上显示的表格数量是动态的。假设数据库中有4个用户,我将显示4个表,每个表包含一个用户的数据。

我试图以编程方式将表添加到我的LinearLayout。但绝对没有在页面上显示任何内容。这是我用于表的代码:

TableRow tr = new TableRow( this );
tr.setLayoutParams(newTableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));

/* Create a Button to be the row-content. */
Button b = new Button( this );
b.setText("Dynamic Button");
b.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

/* Add Button to row. */
tr.addView(b);

/* Add row to TableLayout. */
TableLayout tl = new TableLayout(getActivity());
tl.addView(tr,new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

LinearLayout linearLayout = (LinearLayout)getActivity().findViewById(R.id.RelativeLayout_schoolAssignments);
linearLayout.addView(tl);

我还考虑过使用GridView。但我无法找到一种在不使用GridviewAdapter的情况下以编程方式向行添加行的方法。 我正在寻找这样的事情:

GridView gv = new GridView( this );
GridRow gr = /* Some code here */;

gv.addRow( gr );

linearLayout.addView( gv );

这种简单的方法存在吗?

0 个答案:

没有答案
相关问题