我的ListView如何只显示我的记录?

时间:2017-04-22 16:39:12

标签: android listview

我的ListView显示了我手机的所有歌曲,

我只想要保存的记录。

如何在ListView中导入我的记录?

我的记录位于我创建的文件“newFolder”

    public class FetchSongs {
        boolean fetchstatus=false;
        ArrayList<File> songs=new ArrayList<File>();

        FetchSongs(){

        }
        public ArrayList<File> findSongs(File root){
            ArrayList<File> al=new ArrayList<File>();
            File[] files=root.listFiles();


            for (File singleFile : files){
                if(singleFile.isDirectory() && !singleFile.isHidden()){
                    al.addAll(findSongs(singleFile));
                }
                else {
                    if(singleFile.getName().endsWith(".mp3")){
                        al.add(singleFile);
                    }
                }
            }
            fetchstatus=true;
            songs=al;
            return al;
        }
        public boolean getfetchstatus(){
            return fetchstatus;
        }
        public ArrayList<File> getsonglist(){
            return songs;
        }
    }

这里我录制音频并保存在此文件夹中“newFolder”

ListView需要只显示此记录。

            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            mRecorder.setOutputFile(mFileName);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

            String sep = File.separator; // Use this instead of hardcoding the "/"
            String newFolder = "FolderName";
            String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
            File myNewFolder = new File(extStorageDirectory + sep + newFolder);
            myNewFolder.mkdir();
            mFileName = Environment.getExternalStorageDirectory().toString()
                    + sep + newFolder + sep + "GRAVAÇÃO-"+ts+".mp3";

1 个答案:

答案 0 :(得分:0)

您可以将getsonglist()findSongs(File root)组合在一起。

public class FetchSongs {
    final String dir;
    boolean fetchstatus;
    List<File> songs=new ArrayList<File>();

    FetchSongs(){
        this("");
    }

    FetchSongs(String dir) {
        this.dir = dir;
    }

    private List<File> getSongList(File root) {
        List<File> al=new ArrayList<File>();
        File[] files=root.listFiles();

        for (File singleFile : files){
            if(singleFile.isDirectory() && !singleFile.isHidden()){
                songs.addAll(findSongs(singleFile));
            }
            else {
                if(singleFile.getName().endsWith(".mp3")){
                    songs.add(singleFile);
                }
            }
        }
    }

    public List<File> getSongList() {
        if (songs.isEmpty()) {
            File root = new File(Environment.getExternalStorageDirectory(), dir);
            fetchstatus=true;
            this.songs.addAll(getSongList(root));
        }
        return songs;
    }
}

然后,您可以尝试使用new FetchSongs("NewFolder").getSongList()

制作适配器