0

我已创建此代码以在我的微调器中创建一个列表。现在我想要按字母顺序排列的列表(例如 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);

      }
    }

请帮帮我!!

4

2 回答 2

0

你检查过文档吗?ArrayAdapter 中有sort 方法。

您只需要实现自己的比较器。我想这只是:

int compare(String s1, String s2) { return s1.compareTo(s2); }

:)

于 2012-12-12T23:39:27.547 回答
0
 addButton.setOnClickListener(new OnClickListener()
 {
      public void onClick(View v)
      {
        addNewSpinnerItem();
        Collections.sort(adapter);
      }
 });

也许?

于 2012-12-12T23:27:38.517 回答