开发者

Simple Android Spinner code [closed]

Closed. This question is off-topic. It is not currently accepting answers.
开发者_高级运维

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 9 years ago.

Improve this question

I have written a simple spinner wrapper, but was wondering if any of you experts out there could think of any ways to make it more robust. It only handles strings at the moment, so that could be the first enhancement...

Anyway, the code for the (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 to hold data values
private ArrayAdapter<String> arrayAdapter;

// add the selected item to the end of the 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 all items from the list and disable it
public void clearItems() {
    arrayAdapter.clear();
    this.setEnabled(false);
}

// make the specified item selected (returns false if item not in the list)
public boolean selectItem(String item) {
    boolean found = false;
    for (int i = 0; i < this.getCount(); i++) {
        if (arrayAdapter.getItem(i) == item) {
            this.setSelection(i);
            found = true;
            break;
        }
    }
    return found;
}

// return the current selected item
public String getSelected() {
    if (this.getCount() > 0) {
        return arrayAdapter.getItem(super.getSelectedItemPosition());
    } else {
        return "";
    }
}

// allow the caller to use a different DropDownView, defaults to android.R.layout.simple_dropdown_item_1line
public void setDropDownViewResource(int resource) {
    arrayAdapter.setDropDownViewResource(resource);
}
// internal routine to set up the array adapter, bind it to the spinner and disable it as it is 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 your XML layout file

2. Set up a variable, mMySpinner = (MySpinner)findViewById(R.id.spinner);

3. You can then use all the functions which should be self-explanatory

4. If there are no items in the list, the spinner is disabled to prevent untoward events

mMySpinner.clearItems()      //to remove all the items  
mMySpinner.addItem("Blue")   //to add Blue as an item in list (items are sorted by abc)
mMySpinner.selectItem("Red") //to make the indicate item the current selection  
mMySpinner.getSelected()     //to return the current selected item string  


I guess there's going to be no more comments so I'll accept this as the answer. The final code is as in the question.

I've been using this in one of my apps and it seems to work fine. Feel free to use it in any of your apps.

-Frink

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜