2010-11-30 56 views
0

我已经写了一个简单的微调包装器,但想知道你们中的任何一位专家是否可以想出任何方法使其更加健壮。 它只是在一瞬间处理字符串,所以这可能是第一个增强...简单的Android微调代码

无论如何,对于代码(可耻严重命名)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); 
} 
} 

要使用:
1.使用a.b.c.MySpinner而不是Spinner在你的XML布局文件
2.设置变量,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 
+1

你为什么要编写自己的排序实现? Arrays.sort()会为你做到这一点。你想要完成什么标准的微调不处理? – I82Much 2010-12-01 00:57:45

回答

1

我想有将是没有更多的评论,所以我会接受这个作为答案。最终的代码与问题中的一样。

我一直在我的应用程序中使用它,它似乎工作正常。随时在任何应用程序中使用它。

-Frink