Java阅读歌曲标题

时间:2014-02-02 04:40:41

标签: java android arraylist hashmap

所以我很困惑,可以真正使用一些帮助 我有一个代码从sdcard获取所有mp3和wav文件,并将它们放在Arraylist hashmap中。我对于试图获得标签中标题而非实际歌曲名称而感到困惑。如果有人也可以帮助我如何将hashmap按字母顺序排序,那将是很棒的。

package com.androidhive.musicplayer;

import android.os.Environment;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;

public class SongsManager {
    final String MEDIA_PATH = Environment.getExternalStorageDirectory()
            .getPath() + "/";
    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    private String mp3Pattern = ".mp3";
    private String wavPattern = ".wav";
    private String flacPattern = ".flac";

    // Constructor
    public SongsManager(){

    }

    /**
     * Function to read all mp3 files and store the details in
     * ArrayList
     * */
    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)) || song.getName().endsWith(wavPattern) || song.getName().endsWith(flacPattern)) {
            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);
        }


    }
}

1 个答案:

答案 0 :(得分:0)

实施Comparator<HashMap<String, String>>可以解决您的问题。

创建一个实现Comparator接口的类,该接口根据任何特定键(您的情况为songTitle)比较HashMaps。我保留了下面的比较级。

class MapComparator implements Comparator<HashMap<String, String>>{

    private String key;

    public MapComparator(String key){
        this.key = key;
    }

    @Override
    public int compare(HashMap<String, String> o1, HashMap<String, String> o2) {
        String val1 = o1.get(key);
        String val2 = o2.get(key);
        return val1.compareTo(val2);
    }

}

现在你所要做的就是在列表返回之前对其进行排序。

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);
                    }
                }
            }
        }
        // Sort your list before returning
        Collections.sort(songsList, new MapComparator("songTitle"));
        return songsList;
    }

现在,您将根据songTitle按排序顺序获取歌曲。希望它能解决你的问题。