英文:
autoCompleteTextView shows results only after deleting a letter
问题
我创建了一个布局,其中包含一个autoCompleteTextView
,应该显示一些书名。
我注意到在某些情况下它不显示任何结果,除非我删除一个字母,然后它才显示正确的结果。
例如,我有一个包含单词Potter的书名字符串数组。
当我输入Potter时,它不显示任何结果:
然而,如果我删除一个字母,突然间它就显示出来了:
我用来填充列表的代码是:
atv_Search.addTextChangedListener( new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) {
一些获取书名的代码
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
Set<String> set = new LinkedHashSet<>( autoBookNames );
autoBookNames.clear();
autoBookNames.addAll( set );
// 直到这一点,一切都正常工作,我可以看到authBookNames不是空的,但在删除之前不显示
listAutoAdapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
atv_Search.setThreshold( 1 );
atv_Search.setAdapter( listAutoAdapter );
}
} );
正如我在代码中所写的,当我进行调试时,即使在删除之前,我确实获得了所有的结果,但由于某种原因它没有显示出来。
有什么原因吗?我的意思是,大多数用户不会想到删除一个字母来显示结果。
谢谢
英文:
I created a layout that has an autoCompleteTextView
in it that should show some names of books.
I noticed that it show no results in some cases unless I delete one letter and then it shows the correct results.
For example, I have an array string of books that has the word Potter in them.
When I type Potter, it shows no results:
However, if I then delete one letter it suddenly shows:
The code im using to populate the list is:
atv_Search.addTextChangedListener( new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) {
SOME CODE TO GET NAMES
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
Set<String> set = new LinkedHashSet<>( autoBookNames );
autoBookNames.clear();
autoBookNames.addAll( set );
// UNTIL THIS POINT IT ALL WORKS GOOD AND I SEE THAT authBookNames IS NOT EMPTY, YET NOT SHOWING UNTIL DELETE
listAutoAdapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
atv_Search.setThreshold( 1 );
atv_Search.setAdapter( listAutoAdapter );
}
} );
As I wrote in the code when I debugged it, I did get all the results even before the deletion but it didn't show it for some reason.
Any reason for this? I mean most users won't think to delete a letter to show the results.
Thank you
答案1
得分: 1
try ==>;
listAutoAdapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
atv_Search.setAdapter( listAutoAdapter );
atv_Search.addTextChangedListener( new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
listAutoAdapter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
} );
let me know if this works
英文:
try==>
listAutoAdapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
atv_Search.setAdapter( listAutoAdapter );
atv_Search.addTextChangedListener( new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
listAutoAdapter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
} );
let me know if this works
答案2
得分: 0
你认为在 “获取名称的一些代码” 中,比较字符串的索引是否错误(例如,最后一个索引小于正确的索引1个字符)
英文:
Do you think that your compare strings' index in "SOME CODE TO GET NAMES" is wrong (such as last index is smaller than the correct one 1 character)
答案3
得分: 0
移动
listAutoAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.item_auto_search, autoBookNames);
atv_Search.setThreshold(1);
atv_Search.setAdapter(listAutoAdapter);
尝试将上述代码移动到 onTextChanged
中,然后测试一次,Ben!
英文:
Move
listAutoAdapter = new ArrayAdapter<String>( getApplicationContext(), R.layout.item_auto_search, autoBookNames );
atv_Search.setThreshold( 1 );
atv_Search.setAdapter( listAutoAdapter );
into onTextchanged and try once, Ben!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论