0

Hi I want to sort my listView items alphabetically. I know there are other questions on SOF regarding this subject, but none of the answers worked for me.

public class myList extends Activity{

// Tab Host
TabHost th;
LinearLayout tab1, tab2;

//List
ListView myListView;
String string_list[] = { "String1", "String2", "String3", "String", "String5", "String6"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    // XML File
    setContentView(R.layout.main_list);

    // TabHost
    initializeTab();

    // ListView
    myListView = (ListView) findViewById (R.id.listView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,string_list);
    myListView.setAdapter(adapter); 

            myListView.setItemsCanFocus(false);
    myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    Collections.sort(myListView, String.CASE_INSENSITIVE_ORDER);
}

I get an error on my Collections.sort statement that says:

The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (ListView, Comparator<String>)

3 Answers 3

1

Change

Collections.sort(myListView, String.CASE_INSENSITIVE_ORDER);

to

Arrays.sort(string_list, String.CASE_INSENSITIVE_ORDER);

and then

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,string_list);
    myListView.setAdapter(adapter); 
Sign up to request clarification or add additional context in comments.

Comments

1

The correct class is the following:

public class myList extends Activity {

    // Tab Host
    TabHost th;
    LinearLayout tab1, tab2;

    //List
    ListView myListView;
    String[] string_list = { "String1", "String2", "String3", "String", "String5", "String6"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        // XML File
        setContentView(R.layout.main_list);

        // TabHost
        initializeTab();

        // ListView
        myListView = (ListView) findViewById (R.id.listView1);
        Arrays.sort(string_list, String.CASE_INSENSITIVE_ORDER);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,string_list);
        myListView.setAdapter(adapter);

        myListView.setItemsCanFocus(false);
        myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }
}

Please note, that Array != Collection

Comments

0

The quickest solution would be to use adapter's sort method:

adapter.sort(String.CASE_INSENSITIVE_ORDER);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.