[英]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.