Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to sort data alphabetically in spinner

Tags:

android

i am new in android field and i want to sort my spinner data alphabetically. please help

note:- i am receiving data from web services.

my code is:-

        <Spinner
            android:id="@+id/spinnerAtlasContactSignup"
            android:layout_width="200dip"
            android:layout_height="46dp"
            android:layout_below="@+id/editCompanySignup"
            android:layout_marginBottom="60dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/slect_box1x"
            android:ems="10"
            android:padding="10dp"
            android:prompt="@string/atlas_contact" />

 private void initializeSpinner(ArrayList<AtlasContact> atlastContacts) {

    ArrayAdapter<AtlasContact> adapter = new ArrayAdapter<AtlasContact>(this,android.R.layout.simple_spinner_item, atlastContacts);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinnerAtlasContact.setAdapter(adapter); 

here AtlasContact is a class which accepts the data coming from web services.

like image 435
Avinash Kumar Pankaj Avatar asked Jan 03 '13 05:01

Avinash Kumar Pankaj


2 Answers

If you are fetching the values from the SQLite DB then their works the query to sort the values alphabetically and put them sorted in an ARRAY.

or Try to add data to the ArrayList and just use the Collections class to sort for you::

Collections.sort(SourceArray);

Also,

Comparable interface and implement the method compareTo()

like image 75
Sam-In-TechValens Avatar answered Nov 14 '22 23:11

Sam-In-TechValens


You just need to sort your source:

Collections.sort(atlastContacts, new Comparator<AtlasContact>(){
  public int compare(AtlasContact a1, AtlasContact a2) {
    return a1.getName().compareToIgnoreCase(a2.getName());
  }
});
like image 25
ashokgelal Avatar answered Nov 14 '22 23:11

ashokgelal