0

我正在尝试在片段内的自定义列表视图中填充 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
    }
}
4

1 回答 1

2
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 ));
 }

in the for loop two things are wrong. The first you create at every iteration a new CustomAdapter. Yoph should crate and set it when the loop runs out. The secondo mistake is that you, at every iteration override the values of Detail. You should create a new Detail object at every iteratio

于 2013-08-01T18:07:37.963 回答