Android Adventures - Spinners And Pickers
Written by Mike James   
Thursday, 19 September 2013
Article Index
Android Adventures - Spinners And Pickers
Array Adapter
Dynamic change

Changing The List

There are lots of more advanced things that you can do with Spinners but these aren't encountered that often - mainly when trying to build a custom user experience. 

The one thing that does occur often is the need to dynamically change the list of items. There are many slight variations on this but essentially what you can do is change the String array and then call the adapter's notifyDataSetChange method. For example if you want to change Mexico, i.e. element one,  to Greenland you would use:

country[1]="Greenland";
stringArrayAdapter.notifyDataSetChanged();

The ArrayAdapter also has an add, clear, remove and an insert method which can be used to modify the underlying data but for this to work the object holding the data has to be modifiable. You can't modify a String array in this way. What you need is an ArrayList. 

If you change the declaration of country to:

ArrayList<String> country=new ArrayList<String>(
     Arrays.asList("Canada", "Mexico", "USA"));

You can then add "Greenland" to the end of the items using:

stringArrayAdapter.add("Greenland");

You can always find out how many items there are using the getCount method. 

Notice that in this case the ArrayAdapter constructor used changes from one that accepts an array to one that accepts a List of objects.

How do you modify a list of items that are created using a resource?

This is a tricky question because the ArrayAdapter creates a String array to hold the data which means you can't use the methods that modify a list. 

There are a number of ways of fixing this up but the simplest is perhaps to construct your own ArrayList from the resource:

Resources res=getResources();
ArrayList<String> country=new ArrayList<String>(
 Arrays.asList(
   res.getStringArray(R.array.SpinnerList)));
ArrayAdapter<String> stringArrayAdapter=
  new ArrayAdapter<String>(
   this,
android.R.layout.simple_spinner_dropdown_item,
   country);

With this version of the ArrayAdapter you can once again use the add and other methods to modify the list of items.

Conclusion 

There is much more to say about the Spinner and how to customize it but the methods explained here are the most common. If you don't agree email me with a question. 

And if you are wondering what happened to Pickers - the next chapter goes into the topic.

Meanwhile if you have any questions on what we've covered so far please let me know using the comments.

 

Androidgears

Android Adventures With Android Studio

 

 

Contents

  1. Getting Started With Android Studio
  2. The Activity And The UI
  3. Building The UI and a Calculator App
  4. Lifecycle and State
  5. Basic Controls And Events
  6. Spinners
  7. Pickers
  8. UI Graphics A Deep Dive
  9. Introducing Fragments
  10. Fragments and XML
  11. Fragment And Activity Working Together
  12. Managing Fragments
  13. Dialog Fragments

See Also:

Custom Projects In Android Studio

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter,FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

blog comments powered by Disqus

 



Last Updated ( Wednesday, 09 April 2014 )
 
 

   
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.