Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to print a array in text view or anything

I have a array of words and I would like to print the array of words onto the screen in a text view after a button is clicked. i was using a for loop to go through the whole list and then set text, but i keep getting a error with that plus it will just replace the last value, so it wont print the whole array. If anybody could explain to me how to do like a g.drawSting() but android version that would be great. my code rt now is not with me but it something like: -I'm a beginner to android btw, probably could tell by this question tho.

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
 }
}
}
like image 671
steven minkus Avatar asked Dec 07 '22 15:12

steven minkus


2 Answers

int arraySize = myArray.size();
for(int i = 0; i < arraySize; i++) {
myTextView.append(myArray[i]);
}

if you want to print one by one, then use \n

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

PS: Whoever suggesting to change .size() to .length(), thanks for you suggestion.

FYI, The questioner mentioned the variable name is array.size() in question, so the answer also having the same variable name, to make it easier for the questioner.

if your variable (myArray) is an Array use myArray.length(), if it is ArrayList use myArray.size()

like image 142
Jayabal Avatar answered Dec 22 '22 09:12

Jayabal


You have to combine all text into a String before you can give it the 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());
}
like image 40
zapl Avatar answered Dec 22 '22 10:12

zapl