我在我的應用程序中有一個自動完成功能。我動態地設置適配器,但是當我點擊其中一個建議的單詞時,我得到一個IndexOutOfBoundsException。我不知道如何解決它。一些幫助將不勝感激。索引越界自動完成android
下面的代碼:
final ArrayList<Person> suggestions = new ArrayList<Person>();
final ArrayList<Person> allPersons = database.selectPersons();
autocomplete = (AutoCompleteTextView)findViewById(R.id.autocomplete);
autocomplete.setThreshold(1);
autocomplete.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before,int count) {
input = autocomplete.getText().toString();
if(input.length() == 0){
suggestions.clear();
input.toUpperCase();
for (Person p : allPersons) {
if(p.getName().startsWith(input)){
suggestions.add(p);
}
}
autocomplete.setAdapter(new ArrayAdapter<Person>(getApplicationContext() , R.layout.listitem , suggestions));
}
if(input.length() > 0){
suggestions.clear();
input = Character.toUpperCase(input.charAt(0)) + input.substring(1);;
for (Person p : allPersons) {
if(p.getName().startsWith(input)){
suggestions.add(p);
}
}
autocomplete.setAdapter(new ArrayAdapter<Person>(getApplicationContext() , R.layout.listitem , suggestions));
}
}
});
autocomplete.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Person input = (Person)arg0.getItemAtPosition(arg2);
Log.d("The person name is",input.getName());
}
});
在此先感謝。
你什麼行IndexOutOfBounds? – Blundell 2011-03-24 11:01:25
我得到它:'Person input =(Person)arg0.getItemAtPosition(arg2); ' – madcoderz 2011-03-24 11:26:22