如何根据编辑文本输入字符对数组值进行排序?

时间:2013-06-08 07:50:58

标签: java android spinner arrays

我有一个String数组,用于在微调器中显示。 现在我的需要是

  

当用户在编辑文本中输入输入时,必须根据编辑文本值对数组进行排序,并且必须在微调器中显示它。我无法完全掌握逻辑。

我试过这样。,

String[] a1 = { "Android", "Apple", "Andrew", "Ball" }, a2 = null;
EditText mn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mn = (EditText) findViewById(R.id.plus);

            a2 = a1;
    mn.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
                            Arrays.sort(a1);
            doCall();
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {

        }

        @Override
        public void afterTextChanged(Editable arg0) {

        }
    });

}

protected void doCall() {

    for (int i = 0; i < a1.length; i++) {
        String s = a1[i];
        for (int j = 0; j < mn.getText().toString().length(); j++) {

            if (Character.toLowerCase(s.charAt(j)) == Character
                    .toLowerCase(mn.getText().toString().charAt(j))) {
                if (j == mn.getText().toString().length() - 1) {
                    String temp = a1[0];
                    a1[0] = a2[i];
                    a1[i] = temp;

                } else {
                    continue;
                }
            } else {
                break;
            }
        }
    }
}

如果我给“a”意味着我得到“安德鲁,球,苹果,Android”  但我需要展示的是“安德鲁,安卓,苹果,球”我怎样才能做到这一点。提前谢谢,

2 个答案:

答案 0 :(得分:1)

您可以在Arrays.sort(names)结束前尝试拨打doCall(),然后通知您的Spinner适配器。

答案 1 :(得分:0)

## AutoComplete ##

尝试使用自动完成功能:

 public class CountriesActivity extends Activity {
 protected void onCreate(Bundle icicle) {
     super.onCreate(icicle);
     setContentView(R.layout.countries);

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
             android.R.layout.simple_dropdown_item_1line, COUNTRIES);
     AutoCompleteTextView textView = (AutoCompleteTextView)
             findViewById(R.id.countries_list);
     textView.setAdapter(adapter);
 }

 private static final String[] COUNTRIES = new String[] {
     "Android", "Apple", "Andrew", "Ball"
 };
}