歌曲在listview中混淆了

时间:2013-10-18 21:29:28

标签: android

我想获取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;


        }
  }

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

2 个答案:

答案 0 :(得分: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...
    }

}

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

希望它有所帮助:]

答案 1 :(得分: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;


      }
}