0

我想获取 android 设备中所有音乐文件的列表,所以我使用了以下代码

public class MusicAdapter extends BaseAdapter {
        private Context mContext;
        private LayoutInflater l_Inflater;

        public MusicAdapter(Context c) {
              mContext = c;
              l_Inflater = LayoutInflater.from(c);
        }

        public int getCount() {
              return count;
        }

        public Object getItem(int position) {
              return position;
        }

        public long getItemId(int position) {
              return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
              System.gc();

            TextView tv = new TextView(mContext.getApplicationContext());
            String id = null;
            TextView txt_itemName = null;

            if (convertView == null) {



convertView = l_Inflater.inflate(R.layout.menu, null);
            txt_itemName = (TextView) convertView.findViewById(R.id.textView1);
            ImageView itemImage = (ImageView) convertView.findViewById(R.id.imageView1);    





                music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
            musiccursor.moveToPosition(position);
            id = musiccursor.getString(music_column_index);


            txt_itemName.setText(id);



            } else  {}




            return convertView;


        }
  }

一切正常但是当我上下滚动时,列表混淆了。我点击一个标题,但它播放错误的标题。每次我回到列表顶部时,都会显示不同的歌曲作为第一首歌曲。我该怎么办.. ??我该如何解决这个问题

4

2 回答 2

1

这是错误的:

    public Object getItem(int position) {
          return position;
    }

    public long getItemId(int position) {
          return position;
    }

getItem你需要返回一个代表你的对象的实例,也许像一个Music对象。并且getItemId您需要为该实例返回一个唯一值。

假设您加载了一个带有Music实例的数组,如下所示:

public class MusicAdapter extends BaseAdapter {

    public class Music {
        private Long id;
        private String pathToSd;
        private Long lenght;
    }

    private List<Music> mMusicList;
    private LayoutInflater mInflater;

    public MusicAdapter(Context context, List<Music> music) {
        mMusicList = music;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mMusicList.size();
    }

    @Override
    public Object getItem(int position) {
        return mMusicList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mMusicList.get(position).id; //If you aren't saving your list of music in some place like a db
                                        //and thus, you don't have an id, you can return -1.
    }

    @Override
    public View getView(int position, View convertedView, ViewGroup parent) {
        if (convertedView == null) {
            convertedView = mInflater.inflate(R.layout.your_layout, null);
        }
        //Perform your operations over the view here...
    }

}

无论如何,我强烈建议您将此列表保存在数据库中。

希望能帮助到你 :]

于 2013-10-18T22:25:33.407 回答
1

这段代码解决了问题

public class MusicAdapter extends BaseAdapter {
      private LayoutInflater l_Inflater;

      public MusicAdapter(Context c) {
            l_Inflater = LayoutInflater.from(c);
      }

      public int getCount() {
            return count;
      }

      public Object getItem(int position) {
            return position;
      }

      public long getItemId(int position) {
            return position;
      }

      public View getView(int position, View convertView, ViewGroup parent) {
            System.gc();

          String id = null;
          TextView txt_itemName = null;

          if (convertView == null) {

  convertView = l_Inflater.inflate(R.layout.menu, null);


 } 




     txt_itemName = (TextView) convertView.findViewById(R.id.textView1);
              convertView.findViewById(R.id.imageView1);    
 music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
              musiccursor.moveToPosition(position);
              id = musiccursor.getString(music_column_index);
              txt_itemName.setText(id);

          return convertView;


      }
}
于 2013-10-19T08:17:31.953 回答