我创建此代码以在我的微调器中创建一个列表。现在我想按字母顺序排列模式(例如xxx,yyy,aaa必须是aaa,xxx,yyy)..我该怎么办?
public class MyAndroidAppActivity extends Activity {
private Spinner spinner;
private EditText Text;
private ArrayAdapter<String> adapter;
private Button addButton;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Text = (EditText) findViewById(R.id.et);
List<String> planets = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.clienti_arrays)));
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, planets);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
Collections.sort(planets);
addButton = (Button) findViewById(R.id.add_new);
addButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
addNewSpinnerItem();
}
});
}
protected void addNewSpinnerItem()
{
String textHolder = "" + Text.getText().toString();
adapter.add(textHolder);
}
}
请帮助我!!
答案 0 :(得分:0)
addButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
addNewSpinnerItem();
Collections.sort(adapter);
}
});
也许?
答案 1 :(得分:0)
你检查过文档吗?有sort method in ArrayAdapter。
您只需要实现自己的比较器。我想这只是:
int compare(String s1, String s2) {
return s1.compareTo(s2);
}
:)