如何在列表视图中显示sd卡中的歌曲列表?

时间:2016-04-09 16:31:25

标签: android listview android-fragments media-player android-sdcard

apk正确构建并安装,但我看不到我的tabview片段中列出的任何歌曲,代码是否正确? 我希望这是从片段中获取歌曲列表的正确方法吗?如果有更好的选择,请解释一下。谢谢

import android.support.v4.app.ListFragment;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

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

class Mp3Filter implements FilenameFilter{
    public boolean accept(File dir, String name){
        return (name.endsWith(".mp3"));
    }
}
public class songs extends ListFragment {

    private static final String SD_PATH = new String("/mnt/sdcard/");
    private List<String> songs= new ArrayList<String>();
    private MediaPlayer mp=new MediaPlayer();


    public songs() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment

        updatePlayList();
        return inflater.inflate(R.layout.fragment_songs, container, false);

    }

    private void updatePlayList() {

        File home= new File(SD_PATH);
        if(home.listFiles(new Mp3Filter()).length>0){
            for(File file: home.listFiles(new Mp3Filter())){
                songs.add(file.getName());
            }
            ArrayAdapter<String> songList=new ArrayAdapter<String>(getActivity(),R.layout.song_item,songs);
            setListAdapter(songList);

        }
    }


}

0 个答案:

没有答案