我正在使用“Android通用音乐播放器”Android应用程序,我想按升序对音乐列表进行排序现在音频随机显示我尝试了多个示例但它对我不起作用请帮助我,如果任何一次可以高贫困。
public class MediaBrowserFragment extends Fragment {
private static final String TAG = LogHelper.makeLogTag(MediaBrowserFragment.class);
private static final String ARG_MEDIA_ID = "media_id";
private BrowseAdapter mBrowserAdapter;
private String mMediaId;
private MediaFragmentListener mMediaFragmentListener;
private View mErrorView, HyperLinkView;
private TextView mErrorMessage, HyperLink;
public Spanned Text;
private final BroadcastReceiver mConnectivityChangeReceiver = new BroadcastReceiver() {
private boolean oldOnline = false;
@Override
public void onReceive(Context context, Intent intent) {
// We don't care about network changes while this fragment is not associated
// with a media ID (for example, while it is being initialized)
if (mMediaId != null) {
boolean isOnline = NetworkHelper.isOnline(context);
if (isOnline != oldOnline) {
oldOnline = isOnline;
checkForUserVisibleErrors(false);
if (isOnline) {
mBrowserAdapter.notifyDataSetChanged();
}
}
}
}
};
// Receive callbacks from the MediaController. Here we update our state such as which queue
// is being shown, the current title and description and the PlaybackState.
private final MediaControllerCompat.Callback mMediaControllerCallback =
new MediaControllerCompat.Callback() {
@Override
public void onMetadataChanged(MediaMetadataCompat metadata) {
super.onMetadataChanged(metadata);
if (metadata == null) {
return;
}
LogHelper.d(TAG, "Received metadata change to media ",
metadata.getDescription().getMediaId());
mBrowserAdapter.notifyDataSetChanged();
}
@Override
public void onPlaybackStateChanged(@NonNull PlaybackStateCompat state) {
super.onPlaybackStateChanged(state);
LogHelper.d(TAG, "Received state change: ", state);
checkForUserVisibleErrors(false);
mBrowserAdapter.notifyDataSetChanged();
}
};
private final MediaBrowserCompat.SubscriptionCallback mSubscriptionCallback =
new MediaBrowserCompat.SubscriptionCallback() {
@Override
public void onChildrenLoaded(@NonNull String parentId,
@NonNull List<MediaBrowserCompat.MediaItem> children) {
try {
LogHelper.d(TAG, "fragment onChildrenLoaded, parentId=" + parentId +
" count=" + children.size());
checkForUserVisibleErrors(children.isEmpty());
mBrowserAdapter.clear();
for (MediaBrowserCompat.MediaItem item : children) {
mBrowserAdapter.add(item);
}
mBrowserAdapter.notifyDataSetChanged();
} catch (Throwable t) {
LogHelper.e(TAG, "Error on childrenloaded", t);
}
}
@Override
public void onError(@NonNull String id) {
LogHelper.e(TAG, "browse fragment subscription onError, id=" + id);
Toast.makeText(getActivity(), R.string.error_loading_media, Toast.LENGTH_LONG).show();
checkForUserVisibleErrors(true);
}
};
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// If used on an activity that doesn't implement MediaFragmentListener, it
// will throw an exception as expected:
mMediaFragmentListener = (MediaFragmentListener) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LogHelper.d(TAG, "fragment.onCreateView");
View rootView = inflater.inflate(R.layout.fragment_list, container, false);
//FooterLink - Rehan
HyperLinkView = rootView.findViewById(R.id.textView1);
HyperLink = (TextView) HyperLinkView.findViewById(R.id.textView1);
Text = Html.fromHtml("" + "<a href='http://www.example.com/'>example</a>");
HyperLink.setMovementMethod(LinkMovementMethod.getInstance());
HyperLink.setText(Text);
//#FooterLink - Rehan
mErrorView = rootView.findViewById(R.id.playback_error);
mErrorMessage = (TextView) mErrorView.findViewById(R.id.error_message);
mBrowserAdapter = new BrowseAdapter(getActivity());
ListView listView = (ListView) rootView.findViewById(R.id.list_view);
listView.setAdapter(mBrowserAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
checkForUserVisibleErrors(false);
MediaBrowserCompat.MediaItem item = mBrowserAdapter.getItem(position);
mMediaFragmentListener.onMediaItemSelected(item);
}
});
return rootView;
}
@Override
public void onStart() {
super.onStart();
// fetch browsing information to fill the listview:
MediaBrowserCompat mediaBrowser = mMediaFragmentListener.getMediaBrowser();
LogHelper.d(TAG, "fragment.onStart, mediaId=", mMediaId,
" onConnected=" + mediaBrowser.isConnected());
if (mediaBrowser.isConnected()) {
onConnected();
}
// Registers BroadcastReceiver to track network connection changes.
this.getActivity().registerReceiver(mConnectivityChangeReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
@Override
public void onStop() {
super.onStop();
MediaBrowserCompat mediaBrowser = mMediaFragmentListener.getMediaBrowser();
if (mediaBrowser != null && mediaBrowser.isConnected() && mMediaId != null) {
mediaBrowser.unsubscribe(mMediaId);
}
MediaControllerCompat controller = ((FragmentActivity) getActivity())
.getSupportMediaController();
if (controller != null) {
controller.unregisterCallback(mMediaControllerCallback);
}
this.getActivity().unregisterReceiver(mConnectivityChangeReceiver);
}
@Override
public void onDetach() {
super.onDetach();
mMediaFragmentListener = null;
}
public String getMediaId() {
Bundle args = getArguments();
if (args != null) {
return args.getString(ARG_MEDIA_ID);
}
return null;
}
public void setMediaId(String mediaId) {
Bundle args = new Bundle(1);
args.putString(MediaBrowserFragment.ARG_MEDIA_ID, mediaId);
setArguments(args);
}
// Called when the MediaBrowser is connected. This method is either called by the
// fragment.onStart() or explicitly by the activity in the case where the connection
// completes after the onStart()
public void onConnected() {
if (isDetached()) {
return;
}
mMediaId = getMediaId();
if (mMediaId == null) {
mMediaId = mMediaFragmentListener.getMediaBrowser().getRoot();
}
updateTitle();
// Unsubscribing before subscribing is required if this mediaId already has a subscriber
// on this MediaBrowser instance. Subscribing to an already subscribed mediaId will replace
// the callback, but won't trigger the initial callback.onChildrenLoaded.
//
// This is temporary: A bug is being fixed that will make subscribe
// consistently call onChildrenLoaded initially, no matter if it is replacing an existing
// subscriber or not. Currently this only happens if the mediaID has no previous
// subscriber or if the media content changes on the service side, so we need to
// unsubscribe first.
mMediaFragmentListener.getMediaBrowser().unsubscribe(mMediaId);
mMediaFragmentListener.getMediaBrowser().subscribe(mMediaId, mSubscriptionCallback);
// Add MediaController callback so we can redraw the list when metadata changes:
MediaControllerCompat controller = ((FragmentActivity) getActivity())
.getSupportMediaController();
if (controller != null) {
controller.registerCallback(mMediaControllerCallback);
}
}
private void checkForUserVisibleErrors(boolean forceError) {
boolean showError = forceError;
// If offline, message is about the lack of connectivity:
if (!NetworkHelper.isOnline(getActivity())) {
mErrorMessage.setText(R.string.error_no_connection);
showError = true;
} else {
// otherwise, if state is ERROR and metadata!=null, use playback state error message:
MediaControllerCompat controller = ((FragmentActivity) getActivity())
.getSupportMediaController();
if (controller != null
&& controller.getMetadata() != null
&& controller.getPlaybackState() != null
&& controller.getPlaybackState().getState() == PlaybackStateCompat.STATE_ERROR
&& controller.getPlaybackState().getErrorMessage() != null) {
mErrorMessage.setText(controller.getPlaybackState().getErrorMessage());
showError = true;
} else if (forceError) {
// Finally, if the caller requested to show error, show a generic message:
mErrorMessage.setText(R.string.error_loading_media);
showError = true;
}
}
mErrorView.setVisibility(showError ? View.VISIBLE : View.GONE);
LogHelper.d(TAG, "checkForUserVisibleErrors. forceError=", forceError,
" showError=", showError,
" isOnline=", NetworkHelper.isOnline(getActivity()));
}
private void updateTitle() {
if (MediaIDHelper.MEDIA_ID_ROOT.equals(mMediaId)) {
mMediaFragmentListener.setToolbarTitle(null);
return;
}
MediaBrowserCompat mediaBrowser = mMediaFragmentListener.getMediaBrowser();
mediaBrowser.getItem(mMediaId, new MediaBrowserCompat.ItemCallback() {
@Override
public void onItemLoaded(MediaBrowserCompat.MediaItem item) {
mMediaFragmentListener.setToolbarTitle(
item.getDescription().getTitle());
}
});
}
// An adapter for showing the list of browsed MediaItem's
private static class BrowseAdapter extends ArrayAdapter<MediaBrowserCompat.MediaItem> {
public BrowseAdapter(Activity context) {
super(context, R.layout.media_list_item, new ArrayList<MediaBrowserCompat.MediaItem>());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MediaBrowserCompat.MediaItem item = getItem(position);
int itemState = MediaItemViewHolder.STATE_NONE;
if (item.isPlayable()) {
itemState = MediaItemViewHolder.STATE_PLAYABLE;
MediaControllerCompat controller = ((FragmentActivity) getContext())
.getSupportMediaController();
if (controller != null && controller.getMetadata() != null) {
String currentPlaying = controller.getMetadata().getDescription().getMediaId();
String musicId = MediaIDHelper.extractMusicIDFromMediaID(
item.getDescription().getMediaId());
if (currentPlaying != null && currentPlaying.equals(musicId)) {
PlaybackStateCompat pbState = controller.getPlaybackState();
if (pbState == null ||
pbState.getState() == PlaybackStateCompat.STATE_ERROR) {
itemState = MediaItemViewHolder.STATE_NONE;
} else if (pbState.getState() == PlaybackStateCompat.STATE_PLAYING) {
itemState = MediaItemViewHolder.STATE_PLAYING;
} else {
itemState = MediaItemViewHolder.STATE_PAUSED;
}
}
}
}
return MediaItemViewHolder.setupView((Activity) getContext(), convertView, parent,
item.getDescription(), itemState);
}
}
public interface MediaFragmentListener extends MediaBrowserProvider {
void onMediaItemSelected(MediaBrowserCompat.MediaItem item);
void setToolbarTitle(CharSequence title);
}
}
答案 0 :(得分:0)
在您调用public void onChildrenLoaded(@NonNull String parentId, @NonNull List<MediaBrowserCompat.MediaItem> children)
的地方,您首先需要使用Collections.sort()
将列表排序到列表中进行排序,并使用比较器来定义您要排序的键。列表(歌曲名称,艺术家姓名等)。
它可能看起来像这样:
Collections.sort(children, new Comparator<MediaBrowserCompat.MediaItem>() {
@Override
public int compare(MediaBrowserCompat.MediaItem lhs, MediaBrowserCompat.MediaItem rhs) {
return lhs.getSongTitle().compareTo(rhs.getSongTitle());
}
});
答案 1 :(得分:0)
尝试使用此方法对MediaBrowserCompat.MediaItem进行排序;
Collections.sort(yourList, new Comparator<MediaBrowserCompat.MediaItem>() {
@Override
public int compare(MediaBrowserCompat.MediaItem lhs, MediaBrowserCompat.MediaItem rhs) {
return lhs.getDescription().getTitle().toString().compareTo(rhs.getDescription().getTitle().toString());
}
});
如果您还想按其他方式对列表进行排序,只需在getDescription().blablabla.compareTo.....
后面键入