8

Tôi biết có thể thêm một số bố cục như ButtonTextView thành LinearLayout. Tôi muốn tạo nhiều hơn một TextView dựa trên điều kiện của vòng lặp for.Cách thêm nhiều dynamicView TextView vào định nghĩa LinearLayout trong tệp main.xml

Tôi đã thử nó theo cách riêng của mình, nhưng tôi không thể tạo ra nó. Có ai biết cách tạo ra nó không?

Vòng lặp sẽ khác nhau tùy theo điều kiện. Xin hãy giúp tôi về việc này. Có thể tạo mảng TextView không?

Tôi đã đã đặt cách bố trí với

setContentView(R.layout.result_page); 

Tôi đang sử dụng chức năng để thêm điểm cho rằng quan điểm hiện tại của tôi:

Chức năng là:

public void addAll() 
{ 

    LinearLayout layout = (LinearLayout)findViewById(R.id.myLayout);  
    layout.setOrientation(1);  
    TextView name[] = null;  
    TextView website[] = null;  
    TextView category[] = null;   
    for (int i = 0; i < 5; i++) 
    {   
     name[i] = new TextView(this);   
     name[i].setText("Name = Shreyash");   
     website[i] = new TextView(this);   
     website[i].setText("Website = shreyah.co.cc");   
     category[i] = new TextView(this);   
     category[i].setText("Website Category = OWN");   
     layout.addView(name[i]);   
     layout.addView(website[i]);   
     layout.addView(category[i]);  
    } 
} 

Nhưng sau nếu tôi chạy Ứng dụng, nó cho tôi thấy lỗi như:

09-08 11:03:28.755: ERROR/AndroidRuntime(318): FATAL EXCEPTION: main 
09-08 11:03:28.755: ERROR/AndroidRuntime(318): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.quiz.spellingquiz/com.quiz.spellingquiz.ResultDisplayPage}: java.lang.NullPointerException 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at android.os.Handler.dispatchMessage(Handler.java:99) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at android.os.Looper.loop(Looper.java:123) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at java.lang.reflect.Method.invoke(Method.java:521) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at dalvik.system.NativeStart.main(Native Method) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318): Caused by: java.lang.NullPointerException 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at com.quiz.spellingquiz.ResultDisplayPage.addAll(ResultDisplayPage.java:59) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at com.quiz.spellingquiz.ResultDisplayPage.onCreate(ResultDisplayPage.java:34) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
09-08 11:03:28.755: ERROR/AndroidRuntime(318):  ... 11 more 

Tôi không biết mình đã sai ở đâu.

hãy giúp tôi về việc này.

tôi đã có kết quả với mã DeeV của như thế này: enter image description here

Nhưng tôi muốn thiết Lời dưới chữ tag, và trả lời dưới câu trả lời tag. Nhưng làm thế nào nó có thể? Tôi nghĩ tôi phải đặt một LinearLayout khác cho điều đó. Anybudy có thể cho tôi biết cách tôi đặt một LinearLayout khác cho kiểu xuất hiện đó không?

+0

Đăng mã của bạn cho đến thời điểm này – Jack

+0

Vui lòng thêm một số mã mẫu để hiển thị những gì bạn đã thử. Điều này sẽ giúp chúng ta thấy những gì không hiệu quả. – elevine

+0

@Jack: Vui lòng xem câu hỏi được cập nhật của tôi. –

Trả lời

11

Bạn chỉ cần tạo một mảng nếu bạn cần thay đổi TextViews sau đó. Tuy nhiên, nếu bạn cần tạo một mảng, thì một cái gì đó như thế này sẽ hoạt động.

List<TextView> textList = new ArrayList<TextView>(NUM_OF_TEXTS); 
for(int i = 0; i < NUM_OF_TEXTS; i++) 
{ 
    TextView newTV = new TextView(context); 
    newTV.setText("New message."); 
    newTV.setTextColor(0xFFFF0000); 
    /**** Any other text view setup code ****/ 
    myLinearLayout.addView(newTV); 
    textList.add(newTV); 
} 

Nếu văn bản được tĩnh một lần tạo ra, sau đó bạn có thể chỉ cần loại bỏ bất kỳ tham chiếu đến các danh sách trong mã, và nó vẫn sẽ được bổ sung vào LinearLayout.

EDIT:

Giả sử tôi hiểu đúng câu hỏi của bạn, bạn muốn bố trí để có một cái gì đó như thế này:

Word: 
Big 
Answer: 
42 

Word: 
Small 
Answer: 
Tough 

Word: 
Example 
Answer: 
Another Answer 

Trong trường hợp đó, bạn có nghĩa là không cần phải làm nhiều. LinearLayout sẽ đặt mọi thứ theo thứ tự bạn đặt với addView. Để cập nhật mã trước đây của tôi, điều này sẽ làm việc:

List<TextView> wordList = new ArrayList<TextView>(NUM_OF_WORDS); 
List<TextView> answerList = new ArrayList<TextView>(NUM_OF_ANSWERS); 

for(int i = 0; i < NUM_OF_WORDS; i++){ 
    TextView blankText = new TextView(context); 
    TextView wordText = new TextView(context); 
    TextView answerText = new TextView(context); 
    blankText.setText(" "); 
    wordText.setText("Word:"); 
    answerText.setText("Answer:"); 

    TextView newWord = new TextView(context); 
    newWord.setText(**** some method of getting the word ****); 
    TextView newAnswer = new TextView(context); 
    newAnswer.setText(**** some method of getting the answer ****); 
    /**** Any other text view setup code ****/ 

    myLinearLayout.addView(wordText); 
    myLinearLayout.addView(newWord); 
    myLinearLayout.addView(answerText); 
    myLinearLayout.addView(newAnswer); 
    myLinearLayout.addView(blankText); 

    wordList.add(newWord); 
    answerList.add(newAnswer); 
} 
+0

Vui lòng xem Câu hỏi đã cập nhật của tôi. Trường hợp tôi sai? –

+0

Ok Mã của bạn hoạt động. nhưng tôi muốn tất cả các textview vào Vertical Directionation. Và nền của bố cục Toàn bộ là transperant nên tôi có thể hiển thị thiết kế hoặc nền của tôiMainLayout. thì làm thế nào nó có thể được? –

+0

Xin chào DeeV, tôi đã triển khai mã của bạn. nó hoạt động nhưng tôi muốn một số thay đổi để làm. Vui lòng xem Câu hỏi đã chỉnh sửa của tôi. Và hướng dẫn tôi về nó. Cảm ơn. –

2
 LinearLayout lila = new LinearLayout(this); 
     ArrayList<Button> alb = new ArrayList<Button>(); 
     int nButton = 10; 
     for (int i = 0; i < nButton; i++) 
     { 
      alb.add(new Button(this)); 
      lila.addView(alb.get(i)); 
     } 
     //works the same way with TextView 
     alb.get(5).setText("myButton"); 

Có lẽ điều này có thể giúp đỡ.

EDIT: Rất tiếc, giống như DeeV.

+0

là nó hoạt động? –

+0

Vui lòng xem Câu hỏi đã cập nhật của tôi. Trường hợp tôi sai? –