开发者

How to add multiple TextView dynamically to the define LinearLayout in main.xml

开发者 https://www.devze.com 2023-04-03 10:45 出处:网络
I know it\'s possible to add some layouts like Button and TextView to a LinearLayout.I want to create more then one TextView based on the condition of a for loop.

I know it's possible to add some layouts like Button and TextView to a LinearLayout. I want to create more then one TextView based on the condition of a for loop.

I have tried it with my own way, but I am not able to create it. Does anybody know how to create it?

The for loop will be different based on the condition. Please help me regarding this. Is it possible to create the array of TextView?

I already have set the layout with

setContentView(R.layout.result_page);

I am using Function to add that view to my existing view:

The function is:

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]);     
    } 
}

But after that if I run the Application, it shows me error like:

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/AndroidRun开发者_JAVA技巧time(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

Where am I wrong regarding this?

I got the result with DeeV's code like this:

How to add multiple TextView dynamically to the define LinearLayout in main.xml

But I want to set the Word under the tag word, and Answer under the tag answer. But how it is possible? I think I have to set another LinearLayout for that. How I set another LinearLayout for that type of appearance?


You only need to create an array if you need to change the TextViews later. However, if you do need to make an array, then something like this should work.

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);
}

If the text is static once created, then you can simply remove any reference to the list in code, and it will still be added to the LinearLayout.

EDIT:

Assuming I understand your question right, you want the layout to be something like this:

Word:
Big
Answer:
42

Word:
Small
Answer:
Tough

Word:
Example
Answer:
Another Answer

In that case, you literally don't have to do much. LinearLayout will put everything in the order that you place it with addView. To update my previous code, this should work:

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);
}


        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");

Maybe this could help.

EDIT : Sorry strictly the same as DeeV.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号