实现过滤器方法过滤字符串ArrayList的问题

时间:2016-01-26 21:38:31

标签: java android

我的app使用listview来显示文件夹中的所有音频文件。这是成功的!我现在正在尝试添加一个方法来过滤ArrayList,只显示与用户输入的字符串匹配的歌曲。不幸的是我收到一条消息"无法解析方法toLowerCase(java.util.Locale)"。

我也正在查看有关过滤ArrayLists的其他StackOverFlow帖子,但这些实例不同,我的半初学者大脑对如何将这些答案应用于我的代码感到困惑。

这是整个SongsManager.java文件(过滤方法在最后):

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

public class SongsManager {

    private HashMap<String, String> song;
    private String songString;

    // SDCard Path
    final String MEDIA_PATH = "/storage/extSdCard/music";
    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    // Constructors
    public SongsManager(String song){
        this.songString = songString;
    }

    public SongsManager(){
    }

    /**
     * Function to read all mp3 files from sdcard
     * and store the details in ArrayList
     * */
    public ArrayList<HashMap<String, String>> getPlayList(){
        File home = new File(MEDIA_PATH);

        if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                HashMap<String, String> song = new HashMap<String, String>();
                song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
                song.put("songPath", file.getPath());

                // Adding each song to SongList
                songsList.add(song);
            }
        }
        // return songs playlist_item array
        return songsList;
    }

    public String getSong() {
        return this.songString;
    }

    /**
     * Class to filter files which are having .mp3 extension
     * */
    class FileExtensionFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".mp3") || name.endsWith(".MP3") || name.endsWith(".wma"));
        }
    }

  // Filter Class
public ArrayList<HashMap<String, String>> filter(String searchString) {
    searchString = searchString.toLowerCase(Locale.getDefault());

    songsList.clear();
    songsList = getPlayList();

    //searchString is empty, so show all songs in results
    if (searchString.length() == 0) {

        if (filteredSongsList != null){
            filteredSongsList.clear();
        }
        filteredSongsList = songsList;
    }

    //only return songs that match the search string
    else {

        if (filteredSongsList != null){
            filteredSongsList.clear();
        }

        for (HashMap<String, String> song : songsList) {
            if (song != null) {
                String songTitle = song.get("songTitle");
                if (songTitle.toLowerCase().contains(searchString)) {
                    filteredSongsList.add(song);
                }
            }
        }
    }

    return filteredSongsList;
}

2 个答案:

答案 0 :(得分:0)

只需改变一下:

for (song : songsList)

到此: 的 [编辑]

for (HashMap<String, String s : songsList)

答案 1 :(得分:0)

解决方案:从HashMap中检索songTitle字符串,并将toLowerCase方法应用于songTitle字符串:

// Filter Class
    public ArrayList<HashMap<String, String>> filter(String searchString) {
        searchString = searchString.toLowerCase(Locale.getDefault());

    songsList.clear();
    songsList = getPlayList();

    //searchString is empty, so show all songs in results
    if (searchString.length() == 0) {

        if (filteredSongsList != null){
            filteredSongsList.clear();
        }
        filteredSongsList = songsList;
    }

    //only return songs that match the search string
    else {

        if (filteredSongsList != null){
            filteredSongsList.clear();
        }

        for (HashMap<String, String> song : songsList) {
            if (song != null) {
                String songTitle = song.get("songTitle");
                if (songTitle.toLowerCase().contains(searchString)) {
                    filteredSongsList.add(song);
                }
            }
        }
    }

    return filteredSongsList;
}