Read complete story

Adding your data to spinner in android.

Spinners in android provide a quick way to select one value from a set of predefined values. But the most cmplecated part is how to pass those values to the android and especially when the data is dynamic.

So here we will learn how to add our data to spinner whether its static or dynamic. Firstly we have to declare the spinner in our activity whether in layout.XML file or programatically, its your choice whatever suits you better. The we have to add the adapter that contains our data.

Static Data:

The static data is easy to add just go to res/values folder and search for arrays.xml file , if its not there create one with the tag <resources> now in this file you can add your data as array forexample:

Now you can use these arrays in your spinner with the following code:

And your spinner is ready to use but the situation complicates where you have dynamic data from a site and then you want to shoe that data in spinner.

Dynamic Data:

Dynamic data can be in various forms like List, Map, etc. so we need a way to deal with it. Actually its quite simple just save your data in one of these and then you can convert them to simple array and use them.

Now add your array to spinner

Thats it and your spinner is ready to use. To set listener in spinner add onItemSelectedListener and put the functionality you want to get in the listener.

You can also add dependent spinners like I did below.

 

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close