Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android sort ListView alphabetically

So I'm trying to sort a simple ArrayAdapter for ListView. Here's what I've tried:

ListView lv;
String[] months = {
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
};

ArrayAdapter<String> adapter;
ArrayList<String> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv = (ListView) findViewById(R.id.listView1);
    lv.setFastScrollEnabled(true);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, months);
    lv.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub

    switch (item.getItemId()) {
        case R.id.sortasc:
            list = new ArrayList<String>(Arrays.asList(months));
            Collections.sort(list);
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));
            }
            adapter.clear();
            adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, list);
            adapter.notifyDataSetChanged();
            return true;
        case R.id.sortdesc:

            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

But I'm getting UnsupportedOperationException, what's wrong with this? I really need help. Thanks in advance!

like image 763
Dunkey Avatar asked Oct 30 '13 08:10

Dunkey


People also ask

How to sort ListView items in Android?

This example demonstrate about How to sort Listview in Android using collections. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.


3 Answers

You didn't called setAdapter(adapter), after sorting your items.

Use like below

adapter = new ArrayAdapter<String>(this,                 android.R.layout.simple_list_item_1, list); lv.setAdapter(adapter); adapter.notifyDataSetChanged(); 

instead of

adapter.clear(); // Useless  adapter = new ArrayAdapter<String>(this,                 android.R.layout.simple_list_item_1, list); adapter.notifyDataSetChanged(); 
like image 50
Pankaj Kumar Avatar answered Sep 27 '22 18:09

Pankaj Kumar


Here is the trick...

private void sortAscending () {     List<String> sortedMonthsList = Arrays.asList(months);     Collections.sort(sortedMonthsList);      months = (String[]) sortedMonthsList.toArray(); } 

And for sortAscending, call

sortAscending(); adapter.notifyDatasetChanged(); 

Similarly, you can implement custom Comparator for sort descending as well...

Hope this helps...:)

like image 29
umair.ali Avatar answered Sep 27 '22 20:09

umair.ali


Comparator<String> ALPHABETICAL_ORDER1 = new Comparator<String>() { 
    public int compare(String object1, String object2) {
        int res = String.CASE_INSENSITIVE_ORDER.compare(object1.toString(), object2.toString()); 
        return res;
    }
}; 

Collections.sort(your array name, ALPHABETICAL_ORDER1);
like image 27
deepak825 Avatar answered Sep 27 '22 19:09

deepak825