diff --git a/src/main/java/com/github/nutomic/controldlna/gui/RouteFragment.java b/src/main/java/com/github/nutomic/controldlna/gui/RouteFragment.java index ed325f2..ad7bf58 100644 --- a/src/main/java/com/github/nutomic/controldlna/gui/RouteFragment.java +++ b/src/main/java/com/github/nutomic/controldlna/gui/RouteFragment.java @@ -163,6 +163,7 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements mRouteAdapter = new RouteAdapter(getActivity()); mRouteAdapter.add(MediaRouter.getInstance(getActivity()).getRoutes()); mRouteAdapter.remove(MediaRouter.getInstance(getActivity()).getDefaultRoute()); + mRouteAdapter.sort(RouteAdapter.COMPARATOR); mPlaylistAdapter = new FileArrayAdapter(getActivity()); mListView = (ListView) getView().findViewById(R.id.listview); @@ -246,6 +247,7 @@ public class RouteFragment extends MediaRouteDiscoveryFragment implements } } mRouteAdapter.add(route); + mRouteAdapter.sort(RouteAdapter.COMPARATOR); RouteInfo current = mMediaRouterPlayService.getCurrentRoute(); if (current != null && route.getId().equals(current.getId())) { diff --git a/src/main/java/com/github/nutomic/controldlna/utility/RouteAdapter.java b/src/main/java/com/github/nutomic/controldlna/utility/RouteAdapter.java index 0835398..2c7305b 100644 --- a/src/main/java/com/github/nutomic/controldlna/utility/RouteAdapter.java +++ b/src/main/java/com/github/nutomic/controldlna/utility/RouteAdapter.java @@ -1,8 +1,10 @@ package com.github.nutomic.controldlna.utility; +import java.util.Comparator; import java.util.List; import android.content.Context; +import android.database.DataSetObserver; import android.support.v7.media.MediaRouter.RouteInfo; import android.view.LayoutInflater; import android.view.View; @@ -14,6 +16,16 @@ import com.github.nutomic.controldlna.R; public class RouteAdapter extends ArrayAdapter { + /** + * Sorts routes by name. Call {@code sort(COMPARATOR)} whenever an item is inserted. + */ + public static final Comparator COMPARATOR = new Comparator() { + @Override + public int compare(RouteInfo lhs, RouteInfo rhs) { + return lhs.getName().compareTo(rhs.getName()); + } + }; + public RouteAdapter(Context context) { super(context, R.layout.list_item); }