编辑:正如Blumer所指出的,我没有向表格中添加项目,所以这个问题之所以出现,只是因为我粗心,我没有看到我的错误。
我正在尝试创建一个动态TableLayout,因为我必须从服务器接收结果并根据结果添加行,但是表没有更新。(此外,TableLayout已经有1个初始行,即标题行)。
这是我的代码:
Room[] rooms = State.rooms;
TableLayout tblBookDetails = (TableLayout) findViewById(R.id.tblbookdetails);
for(int i = 0; i < rooms.length; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LayoutParams layout_wrapwrap = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout_wrapwrap.rightMargin = 10; //TODO: Convert into DP
Resources res = getResources();
TextView txt1 = new TextView(this);
txt1.setLayoutParams(layout_wrapwrap);
txt1.setTextColor(res.getColor(android.R.color.black));
txt1.setText(rooms[i].name);
TextView txt2 = new TextView(this);
txt2.setLayoutParams(layout_wrapwrap);
txt2.setTextColor(getResources().getColor(android.R.color.black));
txt2.setText(rooms[i].price + " " + rooms[i].currency);
EditText edit1 = new EditText(this);
edit1.setLayoutParams(layout_wrapwrap);
//Must use deprecated method, since support library does not provide for this.
edit1.setBackgroundDrawable(res.getDrawable(android.R.drawable.edit_text));
edit1.setEms(3);
edit1.setInputType(InputType.TYPE_CLASS_NUMBER);
EditText edit2 = new EditText(this);
edit2.setLayoutParams(layout_wrapwrap);
//Must use deprecated method, since support library does not provide for this.
edit2.setBackgroundDrawable(res.getDrawable(android.R.drawable.edit_text));
edit2.setEms(3);
edit2.setInputType(InputType.TYPE_CLASS_NUMBER);
Spinner spinner = new Spinner(this);
layout_wrapwrap.rightMargin = 0;
spinner.setLayoutParams(layout_wrapwrap);
Integer[] numbers = new Integer[rooms[i].count];
for(int j = 0; j < numbers.length; j++) {
numbers[j] = i + 1;
}
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(
BookActivity.this, R.layout.spinner_textview, numbers);
spinner.setAdapter(adapter);
tblBookDetails.addView(tr);
}
//Another exquisite beauty of Java.
Log.d("USR", Integer.valueOf(tblBookDetails.getChildCount()).toString());
tblBookDetails.invalidate();
tblBookDetails.refreshDrawableState();为了防止混淆,Room[]数组只是一个简单的属性持有器类。这段代码看起来非常复杂,并且表没有更新。我在互联网上搜索了很多,但我找不到任何解决这个问题的方法。
提前谢谢你。
发布于 2012-10-15 22:37:25
我知道您将tr添加到tblBookDetails的位置,但我没有看到您将txt1、txt2、edit1等放在tr中的位置。尝试将这些视图添加到行中,我认为这应该会让您达到目的,因为现在您似乎正在添加TableRow,但其中没有任何内容。
https://stackoverflow.com/questions/12897732
复制相似问题