2012-01-26 28 views
5

Tôi có Hoạt động hiển thị nhận xét. Các bình luận có một bố trí, vì vậy tôi không thể chỉ sử dụng một ListView.Thêm chế độ xem vào LinearLayout theo lập trình

Tôi đang thêm nhận xét bằng vòng lặp và chương trình đi qua toàn bộ vòng lặp (được kiểm tra qua LogCat), nhưng chỉ thêm Chế độ xem (nhận xét) đầu tiên vào linearlayout.

Mã của tôi (trên thực tế tham số fillComments sẽ là một cái gì khác hơn là 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); 
    } 
} 
+0

Nó cũng sẽ xảy ra nếu 'R.id.comments_area' có hướng dọc + chiều rộng fill_parent? Và nhật ký ("Adder comment") có hiển thị nhiều lần không? – louiscoquio

+0

bạn có thể vui lòng chỉ cho chúng tôi bố cục: comment_row_layout.xml .. Tôi có thể đoán bạn có định hướng ngang ... –

Trả lời

6

tôi nghĩ mCommentArea = (LinearLayout) findViewById(R.id.comments_area); hướng bố cục này là Ngang để vấn đề này xảy ra. xin vui lòng nếu định hướng ngang sau đó xin vui lòng thay đổi nó theo chiều dọc và tận hưởng

+0

Bạn vừa tiết kiệm cho tôi rất nhiều thời gian cho việc này, chúc mừng! – ryanwils

2

Làm thế nào bạn đã xác định LinearLayout? Nó có thể chỉ là một vấn đề hiển thị. Kiểm tra kích thước và hướng của LinearLayout.

+0

Bạn nói đúng, tôi đã quên thêm dòng 'android: orientation =" vertical "' vào LinearLayout của tôi. – j0ntech

Các vấn đề liên quan