Reputation: 87
I've been searching around StackOverFlow, for answers. But none seemed to really help me? I have made a successful Music Player, everything is great but one thing annoys me. It's that Song names don't appear in alphabetical order. So my question is how I can arrange the music according to to "abc" format?
Also, I don't have my ListView set up as an Activity (Java). It's set up in my main.xml.
Code for when the Music is retrieved from my device -
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);
}
}
}
Code for when it's applied in MainActivity -
songsList = songManager.getPlayList();
Thanks if you can help me out :)
Upvotes: 2
Views: 530
Reputation: 328
That should work:
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);
}
}
}
}
Upvotes: 1
Reputation: 12042
Sort the ArrayList
before creating the arrayadapter
and then set the arrayadater
to the ListView
Use Collections.sort to sort your ArrayList
Collections.sort(arraylist)
Upvotes: 2