2010-07-09 56 views
12

Tôi đang cố thêm Chế độ xem vào bố cục tuyến tính theo chương trình.Android: Thêm hai chế độ xem văn bản theo chương trình

LinearLayout layout  = (LinearLayout) findViewById(R.id.info); 
    String [] informations = topOffer.getInformations(); 
    TextView informationView; 
    View line = new View(this); 
    line.setLayoutParams(new LayoutParams(1, LayoutParams.FILL_PARENT)); 
    line.setBackgroundColor(R.color.solid_history_grey); 
    for (int i = 0; i < informations.length; i++) { 
     informationView = new TextView(this); 
     informationView.setText(informations[i]); 
     layout.addView(informationView, 0); 
     layout.addView(line, 1); 
    } 

Trước tiên, tôi chỉ thêm thông tin Xem và mọi thứ hoạt động tốt. Mông sau khi thêm cũng là dòng-Xem, nó bị lỗi với các lỗi sau đây:

java.lang.IllegalStateException: Con được chỉ định đã có một phụ huynh. Bạn phải gọi removeView() trên phụ huynh của đứa trẻ trước.

Vì vậy, tôi đã thử addView (Xem v, int index), nhưng nó đã bị lỗi với cùng một thông báo ...

Có ai đó là giải pháp không?

Cảm ơn, Martin

Trả lời

2

Bạn không thể thêm quan điểm tương tự con nhiều lần trong cùng một cửa mẹ. Bạn cần tạo chế độ xem mới hoặc tăng lượt xem mới mỗi lần.

12

Như gpmoo7 nói rằng bạn cần phải tạo ra mỗi khi một cái nhìn mới trong vòng lặp

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.linear); 

    LinearLayout layout = (LinearLayout) findViewById(R.id.linear); 

    String[] informations = new String[] { "one", "two", "three" }; 
    TextView informationView; 

    for (int i = 0; i < informations.length; i++) { 
     View line = new View(this); 
     line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT)); 
     line.setBackgroundColor(0xAA345556); 
     informationView = new TextView(this); 
     informationView.setText(informations[i]); 
     layout.addView(informationView, 0); 
     layout.addView(line, 1); 
    } 

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