0

I'm sure this has already been asked, so if somebody can refer me to another thread that'd be great.

I've got a string array:

    String animalArray[] = {"Dog", "Cat", "Horse", "Snake"};

And I've got a spinner with an adapter. How do I avoid having to do this?:

    adapter1.add(animalArray[0]);
    adapter1.add(animalArray[1]);
    adapter1.add(animalArray[2]);
    adapter1.add(animalArray[3]);

Can I use a for loop or something? Or is there a better way?

2 Answers 2

1

Loop through your array and add them like this:

for (String s : animalArray) {
    adapter1.add(s);
}
Sign up to request clarification or add additional context in comments.

Comments

0

ArrayAdapter(if you are talking about it) counstructor can solve the problem. It can use String array as data sourse, T[] objects.

UPD sample code:

for(int i=0;i<animalArray.length;i++){
   adapter1.add(animalArray[i]);
}

3 Comments

Yeah, but I don't want to use an ArrayAdapter because it doesn't let me add to the String ArrayList from the xml file. I want to be able to have the adapter populate dynamically depending on which animals I put into the string.
Then just loop though String array.
Works great, thanks! For future people who see this, you're missing a ) before the ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.