gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:28:31 25 4
gpt4 key购买 nike

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

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.

我已经尝试过这样的。

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”我怎样才能实现这一点。提前致谢,

最佳答案

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

关于java - 如何根据编辑文本输入字符对数组值进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16997346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com