Search code examples
androidtextview

Cannot set text in a textView from a for-loop in Android?


I am trying to set some text from a for-loop in my textView. I have this List: List<List<String>> movies and the for-loop looks like this:

int i = 0;
for (List<String> listMovies : movies) {
     i++;
     txtViewResult.setText("Movie " + i + " - " + "Number of actors: " + list.size() + "\n");
     txtViewResult.setText("  Actors:\n");
     for (String s : listMovies) {
          txtViewResult.setText("   " + s + "\n");
     }
}

My textView:

<TextView
            android:layout_width="fill_parent"
            android:layout_height="500dp"
            android:id="@+id/tvResult"
            android:singleLine="false"
            android:maxLines="200"
            android:paddingStart="15dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"/>

My problem is I am getting just the last line of the for-loop (the last actor) in the textView?!!!

I have put android:singleLine="false", android:maxLines="200" (just in case) and I am using; go to the new line \n. Nevertheless, I am not getting what I want.

Any idea(s) why I am getting this? Did I miss some attribut for the textView? Any help is very appreciated.


Solution

  • You have to concatenate text before you set it in textView. Now your textView has last name of actor becouse that line is called as last.

         txtViewResult.setText("   " + s + "\n");
    

    Try to do something like this:

    String myText = ""
    for (List<String> listMovies : movies) {
         i++;
         myText += ("Movie " + i + " - " + "Number of actors: " + list.size() + "\n";
         myText += "  Actors:\n";
         for (String s : listMovies) {
           myText += "   " + s + "\n";
         }
    }
    txtViewResult.setText(myText)