Sorting ArrayAdapter for Android

Viewed 16164

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();
}
1 Answers
Related