我正在尝试在片段内的自定义列表视图中填充 mp3files,而片段又是 viewpager 的一部分。这是我要放置列表视图的片段的代码:
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);
SongDetails Detail = null ;
Context _c = null ;
ArrayList<SongDetails> Songinfo;
// AdapterView.AdapterContextMenuInfo info;
Songinfo = new ArrayList<SongDetails>();
File f=new File("/system/");
File[] files = f.listFiles(new Mp3Filter());
if( files.length>0)
{
for(int i=0; i<files.length; i++)
{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(".rc"))|| (name.endsWith(".Mp3")) ||
(name.endsWith(".MP3"));//searching for the files
}
}