按字母顺序排列音乐播放器的播放列表 (Android Studio)
Sorting alphabetically playlist of a music player (Android Studio)
我下载了这个项目:
而且我会按字母顺序对“所有歌曲”部分中的歌曲进行排序。我试着把它放在 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;
}
}
应提供Function
in Comparator.comparing
作为方法参考:
Collections.sort(songList, Comparator.comparing(SongList::getTitle));
我下载了这个项目:
而且我会按字母顺序对“所有歌曲”部分中的歌曲进行排序。我试着把它放在 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;
}
}
应提供Function
in Comparator.comparing
作为方法参考:
Collections.sort(songList, Comparator.comparing(SongList::getTitle));