简体   繁体   English

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

[英]How to Sort array values based on Edit Text input characters?

I am having an array of String which is being used to show in a spinner. 我有一个String数组,用于在微调器中显示。 Now my need is 现在我的需要是

When the user giving their input in the edit text have to sort the array based on the edit text values ans have to show it in the spinner. 当用户在编辑文本中提供输入时,必须根据编辑文本值对数组进行排序,并且必须在微调器中显示该数组。 I cannot get the logic fully. 我无法完全理解逻辑。

I have tried like this., 我已经尝试过了,

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;
            }
        }
    }
}

IF i am giving "a" means i am getting "Andrew,Ball,Apple,Android" but What i need to be shown is "Andrew,Android,Apple,Ball" How can i achieve this. 如果我给“ a”表示我得到“ Andrew,Ball,Apple,Android”,但我需要显示的是“ Andrew,Android,Apple,Ball”。我该如何实现。 thanks in advance, 提前致谢,

您可以尝试在doCall()结束之前调用Arrays.sort(names) doCall() ,然后通知您的Spinner Adapter。

## AutoComplete ## ##自动完成##

Try using the autoComplete functionality: 尝试使用自动完成功能:

 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"
 };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中根据另一个数组的值对一个数组排序? - How to sort one array based on the values of other array in Java? 如何使用预定义值编辑数组以包含用户输入的JAVA - How to edit an array with predefined values to also include user input JAVA 显示数组中每个元素有多少个字符(基于扫描仪输入) - Show how many characters there are in each element in an array (based on scanner input) 基于编辑文本输入的微调器值 - Spinner value based on edit text input 如何根据标签文本对标签的数组列表进行排序? JavaFX - How to sort an array list of labels based on label text? JavaFX 如何检查在JTextField中输入的值并将字符串输入转换为字符数组? - How to check the values inputted in the JTextField and transfer the string input to an array of characters? 根据具有不同值的另一个数组对数组进行排序 - Sort an Array based on Another Array with different values 如何验证android中的特殊字符编辑文本? - How to validate edit text in android for special characters? 如何根据输入值和 Asc/Desc 作为用户输入执行自定义排序 -Java - How to perform a custom sort based on input values and Asc/Desc as user Input -Java 如何基于数组中值的平均值以升序对类的实例进行排序? - How to sort instances of a class based in ascending order based on the average of the values in an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM