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:
1 2 3 4 5 6 7 8 9 |
<string-array name="size"> <item>S</item> <item>M</item> <item>L</item> </string-array> <integer-array name="numbers"> <item>1</item> <item>2</item>.... </integer-array> |
Now you can use these arrays in your spinner with the following code:
1 2 3 4 5 6 7 |
Spinner size_spinner = (Spinner) convertView.findViewById(R.id.spinner1); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.size, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Apply the adapter to the spinner size_spinner.setAdapter(adapter); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//converting ArrayList<String> in String[] ArrayList<String> array_list= new ArrayList<>(); array_list.add("high"); array_list.add("medium"); array_list.add("low"); String[] array= new String[array_list.size()]; array_list.toArray(array); //converting HashMap<Integer, String> in String[] HashMap<Integer, String> hashmap= new HashMap<>(); hashmap.put(1,S); hashmap.put(2,M); hashmap.put(3,L); Collection<String> collection = hashmap.values(); String[] array = collection.toArray(new String[hashmap.size()]); |
Now add your array to spinner
1 2 3 |
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, array); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); my_spinner.setAdapter(adapter); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
size_spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub item = size_spinner.getSelectedItem(); if(item.equals("S") { Collection<String> col = hashmap.values(); String[] array = col.toArray(new String[hashmap.size()]); } else{ String[] array= new String[array_list.size()]; array_list.toArray(array); } ArrayAdapter<CharSequence> adapter2 = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item,array); my_spinner.setAdapter(adapter2); adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); my_spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub // anything you want } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); |