Dunkey
Dunkey

Reputation: 1922

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!

Upvotes: 23

Views: 79290

Answers (8)

Mahbubur Rahman Khan
Mahbubur Rahman Khan

Reputation: 415

Rather than add data directly to your adapter, you can add them in Collection. Than sort the collection by using compactor. and finally...

adapter.addAll(collectionData);

Upvotes: 0

Arshdeep Singh
Arshdeep Singh

Reputation: 89

its simple if you already have another adapter you can just copy paste this code under declaration of adapter

adapter.sort(new Comparator<String>() {
        @Override
        public int compare(String arg1, String arg0) {
            return arg1.compareTo(arg0);
        }
    });

Upvotes: 0

Suriv
Suriv

Reputation: 51

Android sort listview alphabetically.

listAdapter = new AppsAdapter(MainActivity.this,R.layout.content_layout,appList);
        final Collator col = Collator.getInstance();
        listAdapter.sort(new Comparator<ApplicationInfo>() {
            @Override
            public int compare(ApplicationInfo lhs, ApplicationInfo rhs) {
                return col.compare(lhs.loadLabel(packageManager),rhs.loadLabel(packageManager));
            }
        });

Upvotes: 3

deepak825
deepak825

Reputation: 432

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);

Upvotes: 8

umair.ali
umair.ali

Reputation: 2714

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...:)

Upvotes: 15

Anup Cowkur
Anup Cowkur

Reputation: 20553

To sort a list, you need to use a Comparator. You need to sort the underlying collections using a comparator that does alphabetical sorting and then notify the list to update itself.

Here's an example of doing this using an ArrayAdapter : https://stackoverflow.com/a/8920348/1369222

Upvotes: 1

Pankaj Kumar
Pankaj Kumar

Reputation: 82948

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();

Upvotes: 22

  1. Sort your array.
  2. Recreate adapter.
  3. Apply adapter to listview.

Upvotes: 1

Related Questions