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.
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()
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());
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With