4

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
 }
}
}
steven minkus
  • 131
  • 2
  • 2
  • 14

3 Answers3

13
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()

Jayabal
  • 3,619
  • 3
  • 24
  • 32
  • This answer could be wrong because arrays have `.length` property in java and they do not have `.size()` method. – Nagy Vilmos Dec 18 '17 at 09:36
2

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());
}
zapl
  • 63,179
  • 10
  • 123
  • 154
  • it's even better to get the `.size()` call out of the `for` loop so it does not need to evaluate the size each time the loop runs. – zapl Mar 12 '12 at 13:47
  • size is just a getter on a field so that would not make a significant difference (http://stackoverflow.com/questions/4438710/using-collection-size-in-for-loop-comparision) – assylias Mar 12 '12 at 13:50
  • That depends on the implementation of `List`. If it's just an `ArrayList` yes no real difference (although locally caching the size is still faster) But unless you know the implementation details of a class then I'd prefer it that way - unless you are multithreaded and have to assume that `list.size()` might change. – zapl Mar 12 '12 at 13:54
0

I use this no-index solution, just an easy to remember one liner:

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

albertpeiro
  • 356
  • 4
  • 14