ajwest
ajwest

Reputation: 258

Adding entire string array to adapter

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?

Upvotes: 0

Views: 573

Answers (2)

user468311
user468311

Reputation:

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]);
}

Upvotes: 0

Pedantic
Pedantic

Reputation: 5022

Loop through your array and add them like this:

for (String s : animalArray) {
    adapter1.add(s);
}

Upvotes: 1

Related Questions