我有一个展示评论的Activity。这些评论本身有一个布局,所以我不能只使用ListView。
我使用循环添加了评论,程序通过整个循环(通过LogCat检查),但只向线性布局添加了第一个视图(评论)。
我的代码(实际上fillComments参数将是String[]之外的东西):
我使用循环添加了评论,程序通过整个循环(通过LogCat检查),但只向线性布局添加了第一个视图(评论)。
我的代码(实际上fillComments参数将是String[]之外的东西):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.comment_layout);
String[] comments = {"kommentaar 1", "kommentaar 2", "kommentaar 3"};
mTitle = (TextView) findViewById(R.id.comments_title);
mTextArea = (EditText) findViewById(R.id.comment_editor);
mAddButton = (Button) findViewById(R.id.add_comment);
mCommentArea = (LinearLayout) findViewById(R.id.comments_area);
mTitle.setText(getIntent().getStringExtra("name"));
fillComments(comments);
}
private void fillComments(String[] comments) {
View comment;
TextView commentator;
TextView commentDate;
TextView commentText;
LayoutInflater inflater = getLayoutInflater();
for (String s : comments) {
Log.d("Comment adder", "Adding comment " + s);
comment = inflater.inflate(R.layout.comment_row_layout, null);
commentator = (TextView) comment.findViewById(R.id.commentator);
commentDate = (TextView) comment.findViewById(R.id.comment_date);
commentText = (TextView) comment.findViewById(R.id.comment_text);
commentator.setText("Test commentator");
commentDate.setText("12-12-2012");
commentText.setText(s);
mCommentArea.addView(comment);
}
}
R.id.comments_area
的方向是垂直的且宽度为 fill_parent,那么这种情况也会发生吗?并且日志(“Comment adder”)会显示多次吗? - louiscoquio