I have a MultiAutoCompleteTextView which lets you enter in multiple entries and shows you autocomplete suggestions. My issue arises when I submit my data. I am adding any entered strings to the drop down list, but my attempts to sort the data fail. The code that executes on submit:
final private Comparator<String> comp = new Comparator<String>() {
public int compare(String e1, String e2) {
return e1.toString().compareTo(e2.toString());
}
};
((ArrayAdapter<String>) autoCompleteView.getAdapter()).add(getString());
((ArrayAdapter<String>) autoCompleteView.getAdapter()).sort(comp);
The code for what happens on clicking the autoCompleteView:
view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
adapter.getFilter().filter(null);
//adapter.sort(comp);
view.showDropDown();
}
});
Can anyone find anything wrong with what I’m doing?
EDIT: some more info, after incorporating changes from @Sam
private ArrayList<String> array = new ArrayList<String>();
private ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,
array);
private void setUpAutoComplete(final MultiAutoCompleteTextView view)
{
array.add("test string"); // this is successfully added to the drop down list
Collections.sort(array, comp);
adapter.notifyDataSetChanged();
}
private void onSubmit()
{
array.add(getString()); // this fails to add
adapter.notifyDataSetChanged();
}
This is a little vague. But I’ll take a guess.
First you do something redundant:
Since
e1ande2are already Strings you don’t need to callString#toString(). Also this basic String comparator already exists. So you don’t need any of this.A better technique is to sort the List, not the adapter. Simply use Collections’ sorting method:
Notice I changed
adapter.add()tolist.add(). I did this becauseadapter.add()callslist.add()andadapter.notifyDataSetChanged()but the adapter shouldn’t be updated until after the new list is sorted.