按字母顺序排序音乐播放器的播放列表(Android Studio)
Posted
技术标签:
【中文标题】按字母顺序排序音乐播放器的播放列表(Android Studio)【英文标题】:Sorting alphabetically playlist of a music player (Android Studio) 【发布时间】:2021-05-28 12:06:51 【问题描述】:我下载了这个项目:
https://projectworlds.in/android-projects-with-source-code/android-music-player-project-with-source-code/
我会按字母顺序对“所有歌曲”部分中的歌曲进行排序。我试图把它放在 onCreate 中:
Collections.sort(songList, Comparator.comparing((Function<? super Object, ? extends Comparable>) songList));
在此处查看代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener, AllSongFragment.createDataParse, FavSongFragment.createDataParsed, CurrentSongFragment.createDataParsed
private Menu menu;
private ImageButton imgBtnPlayPause, imgbtnReplay, imgBtnPrev, imgBtnNext, imgBtnSetting;
private TabLayout tabLayout;
private ViewPager viewPager;
private SeekBar seekbarController;
private DrawerLayout mDrawerLayout;
private TextView tvCurrentTime, tvTotalTime;
private ArrayList<SongsList> songList;
private int currentPosition;
private String searchText = "";
private SongsList currSong;
private boolean checkFlag = false, repeatFlag = false, playContinueFlag = false, favFlag = true, playlistFlag = false;
private final int MY_PERMISSION_REQUEST = 100;
private int allSongLength;
MediaPlayer mediaPlayer;
Handler handler;
Runnable runnable;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
grantedPermission();
onPrepared();
//Sorting playlist here
Collections.sort(songList, Comparator.comparing((Function<? super Object, ? extends Comparable>) songList));
但应用程序崩溃了。 arraylist 没有对象,所以我不能使用 toCompare(例如:obj 1 和 obj 2)。我想比较 ArrayList 本身>
对此有什么想法吗?提前致谢。
SongList.java:
package com.example.soc_macmini_15.musicplayer.Model;
public class SongsList
private String title;
private String subTitle;
private String path;
public String getPath()
return path;
public void setPath(String path)
this.path = path;
public SongsList(String title, String subTitle, String path)
this.title = title;
this.subTitle = subTitle;
this.path = path;
public String getTitle()
return title;
public void setTitle(String title)
this.title = title;
public String getSubTitle()
return subTitle;
【问题讨论】:
这能回答你的问题吗? Sort ArrayList of custom Objects by property 很遗憾,我的 ArrayList 没有对象。 它没有任何你想要排序的对象吗? 问题是我不太了解整个代码...如果有人可以向我解释我将不胜感激... 你能显示类 SongList 吗? 【参考方案1】:Comparator.comparing
中的 Function
应作为方法参考提供:
Collections.sort(songList, Comparator.comparing(SongList::getTitle));
【讨论】:
以上是关于按字母顺序排序音乐播放器的播放列表(Android Studio)的主要内容,如果未能解决你的问题,请参考以下文章