gpt4 book ai didi

java - AutoCompleteTextview 不显示第一个字符条目的下拉建议

转载 作者:行者123 更新时间:2023-11-29 19:42:37 25 4
gpt4 key购买 nike

我有一个 AutoCompleteTextView searchFriends 和一个 String[] usernames= {"Lex","Lincoln","Luther"}我从数据库中获取。

问题:我第一次在 AutoCompleteTextView 中输入字母“L”时,没有显示任何建议。如果我按退格键删除“L”,然后再次重新键入“L”,则建议使用“Lex”、“Lincoln”和“Luther”。我希望在第一次输入内容时显示建议。

public class SearchFriendsActivity extends AppCompatActivity {

Firebase userbase;

AutoCompleteTextView searchFriends;

ArrayAdapter adapter;

char firstChar;
String url, listOfusers, inputUsername, userss;
String[] usersArray;

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

searchFriends= (AutoCompleteTextView)findViewById(R.id.searchFriends);

searchFriends.addTextChangedListener(new TextWatcher() {

private Timer timer = new Timer();
private final long DELAY = 1000; // milliseconds

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}


@Override
public void afterTextChanged(Editable s) {
timer.cancel();
timer = new Timer();
timer.schedule(
new TimerTask() {
@Override
public void run() {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
if (s.length() == 0) {
firstChar = '.';
} else {
if (firstChar == s.charAt(0)) {
aVoid();
} else {
firstChar = s.charAt(0);
url = @string/databaseString + firstChar.toString() + "/";
userbase = new Firebase(url);
createArray();
}
}
}
});
}
},
DELAY
);

}
});

}

public void createArray() {
userbase.addValueEventListener(new ValueEventListener() {

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
listOfusers = dataSnapshot.getValue().toString();
userss = listOfusers.replaceAll("=.*?\\}", "").replace("{", "").replace("}", "").replace(", ", ",");
usersArray = userss.split(",");

aVoid();
}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
});
}

public void aVoid() {
adapter = new ArrayAdapter(this, R.layout.suggestion_dropdown_item, usersArray);
searchFriends.setThreshold(1);
searchFriends.setAdapter(adapter);
}
}

最佳答案

一旦您在适配器中拥有了整个列表,就这么简单:

    @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Filterable adapter = (Filterable) searchFriends.getAdapter();
adapter.getFilter().filter(s);
}

关于java - AutoCompleteTextview 不显示第一个字符条目的下拉建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38378481/

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