2

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 :)

SmallVille
  • 87
  • 8
  • You have to sort your list. http://stackoverflow.com/questions/1134976/how-may-i-sort-a-list-alphabetically-using-jquery – laymelek Apr 11 '14 at 08:51

3 Answers3

2

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)
LionC
  • 3,106
  • 1
  • 22
  • 31
Nambi
  • 11,944
  • 3
  • 37
  • 49
1

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);
        }
        }
    }
}
dharr
  • 328
  • 1
  • 11
  • Before you return arrayList of songs add: Collections.sort(songList, new Comparator() { @Override public int compare(File o1, File o2) { // TODO Auto-generated method stub return o1.getName().compareTo(o2.getName()); } }); – dharr Apr 11 '14 at 09:15
  • Where am I to implement this code? i added it to my SongsManager, and it doesnt seem to make any changes – SmallVille Apr 11 '14 at 09:59
  • just before return statement in getPlayList() – dharr Apr 11 '14 at 10:03
  • it says - The method sort(List, Comparator super T>) in the type Collections is not applicable for the arguments (ArrayList>, new Comparator(){}) – SmallVille Apr 11 '14 at 10:13
  • Add new method just to add all songs to temp ArrayList There you sort it and pass it to the addSongToList Change addSongToList param to ArrayList Make there a loop to take all files and add them to songsList – dharr Apr 11 '14 at 11:08
  • Ok I make some changes. If there are some mistakes/errors tell me. Basically idea is to first add files to temp array sort them and then pass it to your method when they are already sorted. – dharr Apr 11 '14 at 11:21
  • Thank you so much! it worked, I had to make some minor edits to some areas but it worked :D! Thanks – SmallVille Apr 11 '14 at 11:27
  • No problem at your service sir ;) There is probably better way of doing this. – dharr Apr 11 '14 at 12:06
  • Thanks, doesn't matter. it still does the job ;D – SmallVille Apr 12 '14 at 03:16
0

you can use collections to sort your list:

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());

                }
            });

check this link

Lokesh
  • 5,180
  • 4
  • 27
  • 42