我一直在StackOverFlow周围寻找答案。但似乎没有人真正帮助我?我创造了一个成功的音乐播放器,一切都很棒,但有一件事让我烦恼。歌曲的名字并不按字母顺序出现。所以我的问题是如何根据" abc"来安排音乐。格式?
另外,我没有将ListView设置为Activity(Java)。它在我的main.xml中设置。
从我的设备中检索音乐的代码 -
public ArrayList<HashMap<String, String>> getPlayList() {
System.out.println(MEDIA_PATH);
if (MEDIA_PATH != null) {
File home = new File(MEDIA_PATH);
File[] listFiles = home.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
System.out.println(file.getAbsolutePath());
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
// return songs list array
return songsList;
}
private void scanDirectory(File directory) {
if (directory != null) {
File[] listFiles = directory.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
}
private void addSongToList(File song) {
if (song.getName().endsWith(mp3Pattern)) {
HashMap<String, String> songMap = new HashMap<String, String>();
songMap.put("songTitle",
song.getName().substring(0, (song.getName().length() - 4)));
songMap.put("songPath", song.getPath());
// Adding each song to SongList
songsList.add(songMap);
}
}
}
在MainActivity中应用的代码 -
songsList = songManager.getPlayList();
谢谢,如果你可以帮助我:)
答案 0 :(得分:2)
在创建ArrayList
之前对arrayadapter
进行排序,然后将arrayadater
设置为ListView
使用Collections.sort对ArrayList
Collections.sort(arraylist)
答案 1 :(得分:1)
这应该有效:
public ArrayList<HashMap<String, String>> getPlayList() {
System.out.println(MEDIA_PATH);
if (MEDIA_PATH != null) {
File home = new File(MEDIA_PATH);
File[] listFiles = home.listFiles();
if (listFiles != null && listFiles.length > 0) {
ArrayList<File> temp = new ArrayList<>();
for (File file : listFiles) {
System.out.println(file.getAbsolutePath());
if (file.isDirectory()) {
scanDirectory(file);
} else {
temp.add(file);
}
}
Collections.sort(temp, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
// TODO Auto-generated method stub
return o1.getName().compareTo(o2.getName());
}
});
addSongToList(temp);
}
}
// return songs list array
return songsList;
}
private void scanDirectory(File directory) {
if (directory != null) {
File[] listFiles = directory.listFiles();
if (listFiles != null && listFiles.length > 0) {
ArrayList<File> temp = new ArrayList<>();
for (File file : listFiles) {
if (file.isDirectory()) {
scanDirectory(file);
} else {
temp.add(file);
}
}
Collections.sort(temp, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
// TODO Auto-generated method stub
return o1.getName().compareTo(o2.getName());
}
});
addSongToList(temp);
}
}
}
private void addSongToList(ArrayList<File> temp) {
for(int i = 0; i<temp.size(); i++)
{
if (temp.get(i).getName().endsWith(mp3Pattern)) {
HashMap<String, String> songMap = new HashMap<String, String>();
songMap.put("songTitle",
temp.get(i).getName().substring(0, (temp.get(i).getName().length() - 4)));
songMap.put("songPath", temp.get(i).getPath());
// Adding each song to SongList
songsList.add(songMap);
}
}
}
}
答案 2 :(得分:0)
您可以使用集合对列表进行排序:
Collections.sort(apps, new Comparator<App>() {
@Override
public int compare(App lhs, App rhs) {
//here getTitle() method return app name...
return lhs.getTitle().compareTo(rhs.getTitle());
}
});
检查此link