2010-10-17 88 views
0

試圖實現簡單的字典。我希望在用戶在EditText框中輸入列表時自動滾動到最佳匹配。我不希望它過濾列表。例如,如果用戶在EditText中鍵入「s」,我希望他/他在EditText框下看到的第一個單詞成爲詞典中以「s」開頭的第一個單詞。但是用戶仍然應該能夠上下滑動並能夠看到整個單詞列表。它基本上就像去功能。我使用ArrayList來存儲我的單詞列表。數據位於res/raw/data.xml文件中。這是我的onCreate方法 @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main);Android:試圖在列表中找到一個項目而不過濾列表,但只是滾動到該項目

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 

} 

    }); 
} 

回答

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

謝謝,Yoni!這工作,但我沒有意識到它會滾動得這麼慢。我需要能夠直接跳到所需單詞的東西。你可以想象如果用戶輸入以z開頭的單詞會發生什麼。另外,這個smoothScrollToPosition()不會在EditText下顯示正確的單詞。它實際上顯示在屏幕的底部。你有什麼其他的建議?再次感謝 – iblagoev 2010-10-17 22:37:56

+0

我明白了。我用setSelection(i); – iblagoev 2010-10-17 23:14:54

相關問題