時掛起我正在創建音樂播放器應用程序,並使用光標從本地存儲中獲取歌曲。我成功地展示了歌曲和專輯藝術,但當我滾動列表時,它會掛起。我該怎麼做才能解決這個問題。Listview在我滾動android
這是我的光標:
cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,MediaStore.Audio.AudioColumns.DURATION+">0", null, sortOrder);
這裏是類結合的歌曲和專輯封面與列表視圖字段:
private class MediaCursorAdapter extends SimpleCursorAdapter {
String backgroundColor = "white";
String someOtherBackgroundColor = "#F5F5F5";
public MediaCursorAdapter(Context context, int layout, Cursor c) {
super(context, layout, c,
new String[]{MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE, MediaStore.Audio.AudioColumns.DURATION,MediaStore.Audio.Media.ALBUM_ID},
new int[]{R.id.displayname, R.id.title, R.id.duration,R.id.iv_art});
}
public Bitmap getAlbumart(Context context, Long album_id){
Bitmap bm = null;
BitmapFactory.Options options = new BitmapFactory.Options();
try{
final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
if (pfd != null){
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd, null, options);
pfd = null;
fd = null;
}
} catch(Error ee){}
catch (Exception e) {}
return bm;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
if(cursor.getPosition() % 2 == 0)
{
view.setBackgroundColor(
Color.parseColor(backgroundColor));
}
else
{
view.setBackgroundColor(
Color.parseColor(someOtherBackgroundColor));
}
TextView title = (TextView) view.findViewById(R.id.title);
TextView name = (TextView) view.findViewById(R.id.displayname);
TextView duration = (TextView) view.findViewById(R.id.duration);
ImageView iv_art = (ImageView) view.findViewById(R.id.iv_art);
String a = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
Long l = Long.parseLong(a);
Bitmap art = getAlbumart(songlist.this,l);
if(art!=null)
{
iv_art.setImageBitmap(art);
}
else
{
iv_art.setImageResource(R.mipmap.app_splash_screen_icon);
}
long durationInMs = Long.parseLong(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));
name.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)));
title.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));
Utility d = new Utility();
String durationInMin = d.convertDuration(durationInMs);
duration.setText("" + durationInMin);
view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.listitem, parent, false);
bindView(v, context, cursor);
return v;
}
}
這裏是一個集列表視圖的適配器代碼:
mediaAdapter = new MediaCursorAdapter(this, R.layout.listitem, cursor);
lv_songlist.setAdapter(mediaAdapter);
你的問題是在加載位圖。在緩存中加載位圖。 –
我不知道如何做到這一點,請給我解決方案先生@Dheerubhai – Pedo
使用https://github.com/nostra13/Android-Universal-Image-Loader或任何其他圖像加載庫來加載圖像中的位圖。 –