使用Listview将专辑封面加载到Glide中

时间:2016-11-10 07:33:27

标签: android listview android-glide

  • 加载专辑封面的代码

        if(cursor != null && cursor.moveToFirst()){
    
            int titleColumn = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
            int idColumn = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
            int artistColumn = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
            int albumColumn = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
            long albumID = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ID);
    
            do {
                long thisID = cursor.getLong(idColumn);
    
                String thisTitle = cursor.getString(titleColumn);
                String thisArtist = cursor.getString(artistColumn);
                String thisAlbum = cursor.getString(albumColumn);
    
                Glide.with(this)
                        .load(albumID)
                        .asBitmap()
                        .into() //How do I return a value?
    
                medialist.add(new SongInfo(thisID, thisTitle, thisArtist, thisAlbum, //Bitmap variable));
            }
            while (cursor.moveToNext());
    

我正在尝试使用Glide将专辑封面加载到列表视图中。但是,我遇到了麻烦;

  

medialist.add(新的SongInfo(..);

1 个答案:

答案 0 :(得分:0)

你做错了: 用这个:

medialist.add(new SongInfo(thisID, thisTitle, thisArtist, thisAlbum,albumID));

现在,在listview适配器类中,获取medialist并使用glide方法: 定义:

Context con;

现在在适配器的构造函数中:

public CustomAdapter(List<Medialist> items, Context context) {
        medialist= items;
        this.con = context;
    }

Glide.with(con)
                    .load(medialist.getAlbumID)
                    .asBitmap()
                    .into(imageview)