0

我正在开发一个应用程序,我在其中生成所有 mp3 文件的列表并将其添加到自定义列表中。我有一个程序,但问题是它不返回子文件夹中的文件。

任何人都可以建议我或提供链接或其他东西,以便我可以迭代地做吗?我想迭代地做,因为如果我不这样做,我将不得不在方法之间传递大量信息,这会变得非常混乱。我试着这样做,结果完全糊涂了。

这是代码:

public class FragmentSongs extends Fragment {

    private static final String Sd_Path=new String("/sdcard/");
    private MediaPlayer mp =new MediaPlayer();

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

        View view = inflater.inflate(R.layout.fragment_a, container, false);
        ListView SngList = (ListView) view.findViewById(R.id.SongList);

           ArrayList<SongDetails> Songinfo;
             // AdapterView.AdapterContextMenuInfo info;
              Songinfo = new ArrayList<SongDetails>();

            File f=new File("/sdcard/");
            File[] files = f.listFiles(new Mp3Filter());
            if( files.length>0)
            {
                for(int i=0; i<files.length; i++) 
                {SongDetails detail=new SongDetails();
                    detail.setIcon(R.drawable.ic_launcher);
                 detail.setSong(files[i].getName());
                 detail.setArtist(files[i].getName());
                 detail.setAlbum(files[i].getName());
                 Songinfo.add(detail);
                }  SngList.setAdapter(new CustomAdapter(Songinfo ));
            }
            else  if (files.length == 0)
                return null;
        return view;
    } }
class Mp3Filter implements FilenameFilter{
    public boolean accept(File dir,String name)
    {
    return (name.endsWith(".mp3"))|| (name.endsWith(".Mp3")) ||(name.endsWith(".MP3"));//searching for the files
    }
}
4

1 回答 1

0

向您的活动添加一个单独的方法,该方法执行以下操作:

private ArrayList<SongDetails> getSongsFromDirectory(File f){
    ArrayList<SongDetails> songs = new ArrayList<SongDetails>();
    if (!f.exists() || !f.isDirectory()) return songs;

    File[] files = f.listFiles(new Mp3Filter());

    for (File file : files){
       if (file.isDirectory()){
          songs.addAll(getSongsFromDirectory(file));
       }
       else{
          SongDetails detail=new SongDetails();
          detail.setIcon(R.drawable.ic_launcher);
          String fileName = file.getName();
          detail.setSong(fileName);
          detail.setArtist(fileName);
          detail.setAlbum(fileName);
          songs.add(detail);
       }
    }

    return songs;
}

还将您的过滤器更改为 FileFilter 并返回文件夹

class Mp3Filter implements FileFilter{
    public boolean accept(File file){
       return (file.isDirectory() || file.getName().toUpperCase().endsWith(".MP3"))
    }
}

最后,将 onCreateView 中的代码更改为:

View view = inflater.inflate(R.layout.fragment_a, container, false);
ListView SngList = (ListView) view.findViewById(R.id.SongList);
File f=new File("/sdcard/");
ArrayList<SongDetails> Songinfo = getSongsFromDirectory(f);
if (songinfo.size()>0){
   SngList.setAdapter(new CustomAdapter(Songinfo));
   return view;
}
else return null;

请注意,我没有测试此代码,但它至少应该让您了解如何完成它。

于 2013-08-02T17:02:49.247 回答