Android:尝试在列表中查找项目而不过滤列表,只是滚动到该项目

Android:尝试在列表中查找项目而不过滤列表,只是滚动到该项目,android,Android,正在尝试实现简单的字典。我想在用户在编辑文本框中键入列表时自动滚动到最佳匹配。我不想让它过滤列表。例如,如果用户在EditText中键入“s”,我希望他/她在EditText框下看到的第一个单词是字典中以“s”开头的第一个单词。但是用户应该仍然能够上下滑动,并且能够看到整个单词列表。它基本上就像一个去功能。我使用ArrayList存储我的单词列表。数据位于res/raw/data.xml文件中。这是我的onCreate方法 @凌驾 创建时的公共void(Bundle savedInstanceS

正在尝试实现简单的字典。我想在用户在编辑文本框中键入列表时自动滚动到最佳匹配。我不想让它过滤列表。例如,如果用户在EditText中键入“s”,我希望他/她在EditText框下看到的第一个单词是字典中以“s”开头的第一个单词。但是用户应该仍然能够上下滑动,并且能够看到整个单词列表。它基本上就像一个去功能。我使用ArrayList存储我的单词列表。数据位于res/raw/data.xml文件中。这是我的onCreate方法 @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main)

wordListView=(ListView)findViewById(R.id.wordList);
myEditText=(EditText)findViewById(R.id.myEditText);
words=newarraylist();
arrAdap=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,words);
setAdapter(arrAdap);
试一试{
InputStream inSource=getResources().openRawResource(R.raw.data);
DocumentBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc=builder.parse(内源,空);
NodeList wordsList=doc.getElementsByTagName(“eng-bg”);
int length=wordsList.getLength();

对于(int i=0;i,您的textWatcher应该在
PostTextChanged
中调用ListView的方法。例如,粗略的想法:

String searchString = s.getText().toString(); //get the EditText's current value
// naive brute-force search, you can definitely be smarter about this, maybe using Java Collections binary-search to find the right spot
for (int i=0; i<words.size(); i++) {
    String word = words.get(i).toString() // assuming your Words can call toString()
    if (word.startswith(searchString)) {
        getListView().smoothScrollToPosition(i);
        break;
    }
}
String searchString=s.getText().toString();//获取编辑文本的当前值
//天真的暴力搜索,你肯定可以更聪明,也许可以使用Java集合二进制搜索来找到正确的位置

for(int i=0;iThanks,Yoni!这很有效,但我没意识到它会滚动得这么慢。我需要一个可以直接跳到所需单词的东西。你可以想象如果用户键入一个以z开头的单词会发生什么。还有这个smoothScrollToPosition()没有在编辑文本的正下方显示单词。它实际上在屏幕底部显示。您还有其他建议吗?谢谢,againI收到了。我使用了setSelection(I);
String searchString = s.getText().toString(); //get the EditText's current value
// naive brute-force search, you can definitely be smarter about this, maybe using Java Collections binary-search to find the right spot
for (int i=0; i<words.size(); i++) {
    String word = words.get(i).toString() // assuming your Words can call toString()
    if (word.startswith(searchString)) {
        getListView().smoothScrollToPosition(i);
        break;
    }
}