Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting ArrayAdapter for Android

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();
}
like image 940
Flash Avatar asked Jul 16 '12 19:07

Flash


People also ask

What is use of ArrayAdapter in Android?

You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .

What parameters does ArrayAdapter take?

Parameters. The current context. This value can not be null. The resource ID for the layout file containing a layout to use when instantiating views.


1 Answers

but my attempts to sort the data fail

This is a little vague. But I'll take a guess.

First you do something redundant:

return e1.toString().compareTo(e2.toString());

Since e1 and e2 are already Strings you don't need to call String#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:

List<String> list = new ArrayList<String>();
list.add(getString());
...

Collections.sort(list);
adapter.notifyDataSetChanged();

Notice I changed adapter.add() to list.add(). I did this because adapter.add() calls list.add() and adapter.notifyDataSetChanged() but the adapter shouldn't be updated until after the new list is sorted.

like image 180
Sam Avatar answered Sep 27 '22 18:09

Sam