tags:

views:

24

answers:

1

Trying to implement simple dictionary. I want to make it so while the user is typing in the EditText box the list to scroll automatically to the best match. I don't want it to filter the list. For example if the user types "s" in the EditText I want the first word that s/he sees under the EditText box to be the first word in the dictionary that starts with "s." But the user should still be able to slide up and down and to be able to see the entire list of words. It is basically like a go to functionality. I used ArrayList to store my list of words. The data is in res/raw/data.xml file. Here is my onCreate method @Override 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  A: 

Your textWatcher should call your ListView's smoothScrollToPosition method in afterTextChanged. For example, rough idea:

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;
    }
}
Yoni Samlan
Thanks, Yoni! This worked but I didn't realize it would be scrolling so slowly. I need something that will jump straight to the desired word. You can imagine what will happen if the user types a word that starts with a z. Also this smoothScrollToPosition() doesn't show the word right under the EditText. It actually shows it at the bottom of the screen. Do you have any other suggestions? Thanks again
DFC
I got it. I used setSelection(i);
DFC