0

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

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

我试过这样,

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”意味着我得到“Andrew,Ball,Apple,Android”但我需要展示的是“Andrew,Android,Apple,Ball”我怎样才能做到这一点。提前致谢,

4

2 回答 2

1

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

于 2013-06-08T07:55:33.820 回答
0

##自动完成##

尝试使用自动完成功能:

 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"
 };
}
于 2013-06-08T08:35:00.493 回答