SmallVille
SmallVille

Reputation: 87

How can I set my ListView in alphabetical order?

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

Answers (3)

dharr
dharr

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

Nambi
Nambi

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

Lokesh
Lokesh

Reputation: 5378

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

Upvotes: 0

Related Questions