简单的Android Spinner代码

简单的Android Spinner代码,android,android-layout,spinner,Android,Android Layout,Spinner,我已经写了一个简单的spinner包装器,但我想知道你们这些专家中是否有人能想出任何方法使它更健壮。 它目前只处理字符串,所以这可能是第一次增强 无论如何,MySpinner类的代码是: package a.b.c; import android.content.Context; import android.util.AttributeSet; import android.widget.ArrayAdapter; import android.widget.Spinner; public

我已经写了一个简单的spinner包装器,但我想知道你们这些专家中是否有人能想出任何方法使它更健壮。 它目前只处理字符串,所以这可能是第一次增强

无论如何,MySpinner类的代码是:

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);
}
}

我想不会有更多的评论了,所以我接受这个答案。最后的代码如问题所示

我已经在我的一个应用程序中使用了它,它似乎工作得很好。您可以在任何应用程序中随意使用它


-Frink

为什么要编写自己的排序实现?Arrays.sort()将为您执行此操作。你想完成什么标准微调器无法处理的事情?欢迎评论。我不知道有一个Arrays.sort()方法,我将研究它。恐怕我来自排序风靡一时的一代程序员(我知道我的冒泡排序非常糟糕!)标准微调器使用起来并不简单,因为你必须设置一个数组适配器,而我的数组适配器将其隐藏在内部。整洁,嗯?它还提供selectItem和getSelected方法,并在?Ok上处理空列表。搬到
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