2017-01-16 80 views
0

我有檢索albumArt和使用AsyncTask在遊標適配器中將該藝術設置爲ImageView的問題。 當我打開我的應用程序,它看起來是這樣的: enter image description here如何使用光標適配器中的AsyncTask加載albumArt

然後我向下滾動列表,一切都很好(也,如果我滾動列表不是非常快): enter image description here

然後我滾動列表回到非常beggining(不是很快),它看起來罰款,以及: enter image description here

也許我做錯了什麼,我想,所以我有一個問題:

是否有可能以某種方式解決這個問題,我已經嘗試了很多不同的方法來實現AsyncTask並使其正常工作,但在一天結束時它看起來總是相同的。

下面是光標適配器代碼:

public class AllSongsCursorAdapter extends CursorAdapter { 

public AllSongsCursorAdapter(Context context, Cursor cursor) { 
    super(context, cursor, 0); 
} 

public boolean isCheckBoxVisible; 
private AddToFavouritesArray favouritesArray = AddToFavouritesArray.getInstance(); 

private static class ViewHolder { 
    CheckBox checkBox; 
    TextView title; 
    TextView artist; 
    ImageView albumArt; 

    private ViewHolder(View view) { 

     checkBox = (CheckBox) view.findViewById(R.id.favouritesCheckBox); 
     title = (TextView) view.findViewById(R.id.title); 
     artist = (TextView) view.findViewById(R.id.artist); 
     albumArt = (ImageView) view.findViewById(R.id.albumArt); 
    } 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup viewGroup) { 
    View view = LayoutInflater.from(context).inflate(R.layout.song_item, viewGroup, false); 
    ViewHolder viewHolder = new ViewHolder(view); 
    view.setTag(viewHolder); 
    return view; 
} 

@Override 
public void bindView(View view, Context context, final Cursor cursor) { 

    final ViewHolder viewHolder = (ViewHolder) view.getTag(); 
    if (isCheckBoxVisible) { 
     viewHolder.checkBox.setVisibility(View.VISIBLE); 
    } else { 
     viewHolder.checkBox.setVisibility(View.GONE); 
    } 

    final int position = cursor.getPosition(); 
    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 

      if (b) { 
       favouritesArray.integerArray.add(position); 

      } else { 
       favouritesArray.integerArray.remove(Integer.valueOf(position)); 
      } 
     } 
    }); 

    int songTitle = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE); 
    int songArtist = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST); 

    String currentTitle = cursor.getString(songTitle); 
    String currentArtist = cursor.getString(songArtist); 

    viewHolder.title.setText(currentTitle); 
    viewHolder.artist.setText(currentArtist); 

    //__________________________ALBUM_ART_______________________________________________________ 

    viewHolder.albumArt.setTag(cursor.getPosition()); 

    new AsyncTask<ViewHolder, Void, Bitmap>(){ 

     private ViewHolder viewHolder; 

     @Override 
     protected Bitmap doInBackground(ViewHolder... viewHolders) { 
      viewHolder = viewHolders[0]; 
      int id = cursor.getColumnIndex(MediaStore.Audio.Media._ID); 
      long songId = cursor.getLong(id); 

      Bitmap albumArt = getAlbumId(context, songId); 
      return albumArt; 
     } 

     @Override 
     protected void onPostExecute(Bitmap bitmap) { 
      super.onPostExecute(bitmap); 
      if(bitmap != null) { 
       viewHolder.albumArt.setImageBitmap(bitmap); 
      }else{ 
       viewHolder.albumArt.setImageResource(R.drawable.placeholder); 
      } 
     } 
    }.execute(viewHolder); 
} 

private Bitmap getAlbumId(Context context, long id) { 

    Bitmap albumArt = null; 
    String selection = MediaStore.Audio.Media._ID + " = " + id + ""; 
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{ 
        MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM_ID}, 
      selection, null, null); 
    if (cursor.moveToFirst()) { 
     long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); 

     albumArt = getAlbumArt(context, albumId); 
    } 
    cursor.close(); 
    return albumArt; 

} 

private Bitmap getAlbumArt(Context context, long albumId) { 

    Bitmap albumArt = null; 
    String selection = MediaStore.Audio.Albums._ID + " = " + albumId + ""; 
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null, selection, null, null); 

    if (cursor.moveToFirst()) { 

     int art = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART); 

     String currentArt = cursor.getString(art); 
     albumArt = BitmapFactory.decodeFile(currentArt); 
    } 
    cursor.close(); 
    return albumArt; 
} 

}

是的,當然,沒有的AsyncTask一切工作正常,除了滾動很慢。 預先感謝您!

編輯

在一天結束的時候,這就是它是如何工作的。非常感謝@Orest Savchak,我的迴應票和接受你的答案。非常感謝。 // __________________________ ALBUM_ART_______________________________________________________

int id = cursor.getColumnIndex(MediaStore.Audio.Media._ID); 
    long songId = cursor.getLong(id); 
    String string = getAlbumArtPath(context, songId); 
    if(string!=null) { 
     Picasso.with(context) 
       .load(new File(string)) 
       .into(viewHolder.albumArt); 
    }else{ 
     viewHolder.albumArt.setImageResource(R.drawable.placeholder); 
    } 
} 
private String getAlbumArtPath(Context context, long id) { 

    String selection = MediaStore.Audio.Media._ID + " = " + id + ""; 
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{ 
        MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM_ID}, 
      selection, null, null); 
    if (cursor.moveToFirst()) { 
     long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); 

     return getAlbumArt(context, albumId); 
    } 
    cursor.close(); 
    return null; 

} 

private String getAlbumArt(Context context, long albumId) { 

    String selection = MediaStore.Audio.Albums._ID + " = " + albumId + ""; 
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null, selection, null, null); 

    if (cursor.moveToFirst()) { 

     int art = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART); 

     return cursor.getString(art); 
    } 
    cursor.close(); 
    return null; 
} 
+0

您可能要檢查這個圖像加載庫。請更清楚地描述你確切的問題是什麼。發生了什麼,你不想看到或什麼不工作?你的截圖不要告訴這很聰明。 –

+0

@ Jonas問題是圖像設置不賴於賴特ImageViews。例如,第一張截圖和第三張截圖是在ListView的開始階段製作的,但在第一張截圖上,除了第一張和第三張(在向下滾動並返回開始之後)之外,您可能會在所有項目上看到相同的相冊集albumArts設置它應該如何。 –

回答

2

你應該自己管理自己的任務。您的視圖被回收,因此,對於不同的行有一個視圖,這會導致問題。大多數項目的使用一些庫,爲你自動做 - 容易得多,e.g Picasso

Picasso.with(context).load(new File(getAlbumArtPath(context, songId))).into(viewHolder.albumArt); 

private String getAlbumArtPath(Context context, long id) { 

    String selection = MediaStore.Audio.Media._ID + " = " + id + ""; 
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{ 
        MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM_ID}, 
      selection, null, null); 
    if (cursor.moveToFirst()) { 
     long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); 

     return getAlbumArt(context, albumId); 
    } 
    cursor.close(); 
    return null; 

} 

private String getAlbumArt(Context context, long albumId) { 

    String selection = MediaStore.Audio.Albums._ID + " = " + albumId + ""; 
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, null, selection, null, null); 

    if (cursor.moveToFirst()) { 

     int art = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART); 

     return cursor.getString(art); 
    } 
    cursor.close(); 
    return null; 
} 
+0

@ Orest謝謝你,我已經開始檢查畢加索(感謝您的聯繫)。幾小時之內會盡快試用。謝謝。 –

相關問題