Simple Android Spinner code -


i have written simple spinner wrapper, wondering if of experts out there think of ways make more robust. handles strings @ moment, first enhancement...

anyway, code (shamefully badly named) myspinner class is:

package a.b.c;  import android.content.context; import android.util.attributeset; import android.widget.arrayadapter; import android.widget.spinner;  public class myspinner extends spinner {  // constructors (each calls initialise) public myspinner(context context) {     super(context);     this.initialise(); } public myspinner(context context, attributeset attrs) {     super(context, attrs);     this.initialise(); } public myspinner(context context, attributeset attrs, int defstyle) {     super(context, attrs, defstyle);     this.initialise(); }  // declare object hold data values private arrayadapter<string> arrayadapter;  // add selected item end of list public void additem(string item) {     this.additem(item, true); } public void additem(string item, boolean select) {     arrayadapter.add(item);     this.setenabled(true);     if (select) this.selectitem(item);     arrayadapter.sort(new comparator<string>() {         public int compare(string object1, string object2) {             return object1.compareto(object2);         };     }); }  // remove items list , disable public void clearitems() {     arrayadapter.clear();     this.setenabled(false); }  // make specified item selected (returns false if item not in list) public boolean selectitem(string item) {     boolean found = false;     (int = 0; < this.getcount(); i++) {         if (arrayadapter.getitem(i) == item) {             this.setselection(i);             found = true;             break;         }     }     return found; }  // return current selected item public string getselected() {     if (this.getcount() > 0) {         return arrayadapter.getitem(super.getselecteditemposition());     } else {         return "";     } }  // allow caller use different dropdownview, defaults android.r.layout.simple_dropdown_item_1line public void setdropdownviewresource(int resource) {     arrayadapter.setdropdownviewresource(resource); } // internal routine set array adapter, bind spinner , disable empty private void initialise() {     arrayadapter = new arrayadapter<string>(super.getcontext(), android.r.layout.simple_spinner_item);     arrayadapter.setdropdownviewresource(android.r.layout.simple_dropdown_item_1line);     this.setadapter(arrayadapter);     this.setenabled(false); } } 

to use:
1. use a.b.c.myspinner instead of spinner in xml layout file
2. set variable, mmyspinner = (myspinner)findviewbyid(r.id.spinner);
3. can use functions should self-explanatory
4. if there no items in list, spinner disabled prevent untoward events

mmyspinner.clearitems()      //to remove items   mmyspinner.additem("blue")   //to add blue item in list (items sorted abc) mmyspinner.selectitem("red") //to make indicate item current selection   mmyspinner.getselected()     //to return current selected item string   

i guess there's going no more comments i'll accept answer. final code in question.

i've been using in 1 of apps , seems work fine. feel free use in of apps.

-frink


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -