1

我有带有数组列表的微调器,它工作正常,但我想整理从 a 到 z 的数据(例如:苹果、球、猫、狗......)顺序。我在下面提交我的代码

    ArrayList<String> SourceArray = new ArrayList<String>();
    Spinner Sourcespinner;// = new Spinner(this);
    Sourcespinner = (Spinner)findViewById(R.id.Spinner1);
    ArrayAdapter<String> SourceArrayAdapter = new ArrayAdapter<String>(this,
     android.R.layout.simple_spinner_item,SourceArray);
    SourceArrayAdapter.add("Chennai");
    SourceArrayAdapter.add("Mumbai");
    SourceArrayAdapter.add("Kolkatta");
    SourceArrayAdapter.add("Delhi");
    Sourcespinner.setAdapter(SourceArrayAdapter);`

我不知道如何对此进行排序

4

2 回答 2

1

您可以使用它对数据进行排序

Collections.sort(SourceArray);
于 2012-07-13T10:22:06.513 回答
0

尝试将数据添加到 ArrayList 并使用Collections类为您排序:

Collections.sort(SourceArray);

如果您需要添加自己的对象,他们需要实现Comparable接口并实现方法compareTo()。更改 ArrayList 的数据时,请确保使用以下代码通知适配器可能已添加新数据:

SourceArrayAdapter.notifyDataSetChanged();
于 2012-07-13T10:23:30.813 回答