私は単純なスピナーラッパーを書いていますが、そこにいる専門家の誰かがそれをより堅牢にする方法を考えることができるかどうか疑問に思っていました。シンプルなAndroidスピナーコード
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);
}
}
使用するには::
それは最初強化することができるよう それだけで(恥悪いという名前)MySpinner
クラスのコードがあり、...とにかく、現時点で
を文字列を処理します代わりに、変数を設定し、あなたのXMLレイアウトファイルでSpinner
2の1. a.b.c.MySpinner
、mMySpinner = (MySpinner)findViewById(R.id.spinner);
3.あなたはその後、自己説明
する必要がありますすべての機能を使用することができます4.何の項目がリストに存在しない場合、スピナーは、私は答えとしてこれを受け入れるだろうので、これ以上のコメントはないように起こっていると思い不都合なイベント
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
なぜ独自のソート実装を作成していますか? Arrays.sort()はそれを行います。標準のスピナーが扱っていないことを達成しようとしていますか? – I82Much
コメントに感謝します。私はArrays.sort()メソッドがあるかどうかはわかりませんでしたが、私はそれを調べます。私はプログラマーの世代から、すべての怒りを並べ替えることができます(と私は気泡の種類が非常に貧しいことを知っています!) – FrinkTheBrave
標準のスピナーは使い方が簡単ではありません。それを内部的に隠す。うん、ねえ?また、selectItemメソッドとgetSelectedメソッドを提供し、空のリストを処理します – FrinkTheBrave