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

时间:2010-10-17 18:38:58

标签: android

尝试实现简单的字典。我希望这样做,当用户在EditText框中键入时,列表会自动滚动到最佳匹配。我不希望它过滤列表。例如,如果用户在EditText中键入“s”,我希望他/他在EditText框下看到的第一个单词是字典中以“s”开头的第一个单词。但是用户仍然可以上下滑动,以便能够看到整个单词列表。它基本上就像去功能一样。我用ArrayList来存储我的单词列表。数据位于res / raw / data.xml文件中。这是我的onCreate方法     @覆盖     public void onCreate(Bundle savedInstanceState){         super.onCreate(savedInstanceState);         的setContentView(R.layout.main);

    wordListView = (ListView)findViewById(R.id.wordList);
    myEditText = (EditText)findViewById(R.id.myEditText);

    words = new ArrayList<Word>();

    arrAdap = new ArrayAdapter<Word>(this, android.R.layout.simple_list_item_1, words);

    wordListView.setAdapter(arrAdap);


    try {
     InputStream inSource = getResources().openRawResource(R.raw.data);
     DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

     Document doc = builder.parse(inSource, null);
     NodeList wordsList = doc.getElementsByTagName("eng-bg");
     int length = wordsList.getLength();
     for(int i = 0; i<length; i++) {
      Element entry = (Element)wordsList.item(i);
      Element eng = (Element)entry.getElementsByTagName("english").item(0);
      Element bul = (Element)entry.getElementsByTagName("bulgarian").item(0);
      Element id = (Element)entry.getElementsByTagName("ID").item(0);

      String english = eng.getFirstChild().getNodeValue();
      String bulgarian = bul.getFirstChild().getNodeValue();
      int wordId = Integer.parseInt(id.getFirstChild().getNodeValue());

      Word word = new Word(bulgarian, english, wordId);
      addNewWord(word);
     }
} catch (ParserConfigurationException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
} catch (SAXException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
} catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
}  

wordListView.setOnItemClickListener(new OnItemClickListener(){
 public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
  selectedWord = words.get(pos);
  showDialog(TRANS_DIALOG);
  myEditText.setText(selectedWord.getEnglish());
 }
});

myEditText.addTextChangedListener(new TextWatcher(){
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
     }

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
  int after) {
 // TODO Auto-generated method stub

}

    });
}

1 个答案:

答案 0 :(得分:2)

您的textWatcher应该在afterTextChanged中调用ListView的smoothScrollToPosition方法。例如,粗略的想法:

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;
    }
}