Android how to print an array in text view or whatever

I have an array of words, and I would like to print an array of words on the screen in a text view after clicking a button. I used a for loop to scroll through the whole list and then set the text, but I keep getting an error with it, and it just replaces the last value, so it won’t print the whole array. If someone could explain to me how to do, like g.drawSting (), but the Android version, that would be great. my rt code is now not with me, but it is something like: -I am new to android btw, maybe I could tell tho on this.

public void onCreate(Bundle savedInstanceState) { //code for a button just being pressed{ //goes to two methods to fix the private array{ for(int y=0; y<=array.size()-1; y++){ textArea.setText(aarray.get(y)); //prints all strings in the array } } } 
+4
source share
3 answers
 int arraySize = myArray.size(); for(int i = 0; i < arraySize; i++) { myTextView.append(myArray[i]); } 

if you want to print one by one use \ n

 myTextView.append(myArray[i]); myTextView.append("\n"); 

PS: Who will suggest changing .size () to .length (), thanks for the suggestion.

FYI, the Questioner mentioned the variable name array.size() , so the answer also has the same variable name to make the task easier.

if your variable (myArray) is used by Array , use myArray.length() , if it is ArrayList uses myArray.size()

+7
source

You need to merge all the text into a string before you can pass it to a TextView. Otherwise, you overwrite the text all the time.

 public void onCreate(Bundle savedInstanceState) { StringBuilder sb = new StringBuilder(); int size = array.size(); boolean appendSeparator = false; for(int y=0; y < size; y++){ if (appendSeparator) sb.append(','); // a comma appendSeparator = true; sb.append(array.get(y)); } textArea.setText(sb.toString()); } 
+2
source

I use this diskless solution, just a catchy one liner:

for(File file:list) Log.d(TAG, "list: " + file.getPath());

0
source

Source: https://habr.com/ru/post/1400994/


All Articles