我正在为Android 2.1编写一个简单的音乐应用程序,并偶然发现了这样的问题:
在我的活动中,我有一个ListView,其中包含专辑封面,专辑标题,艺术家和每行中的歌曲数量。我在ContentProvider中查询要放入ListView的所有值。但SDCARD上的一些专辑没有专辑封面,我认为查询返回NULL。结果在我的ListView中,我有专辑,其中只有少数有封面。
我想做的是在查询ContentProvider或其他地方时提出一些参数,如果查询返回null封面,则将我的默认图标作为专辑封面。我的意思是,如果ContentProvider没有返回任何内容,我想将默认图标放在这个位置。
也许我应该使用其他一些查询,而不是ManagedCursor?
我看到使用IFNULL子句解决类似的db问题(SELECT IFNULL(标题,“------”)AS title FROM books;)。我认为它可以在某种程度上提供帮助,但是没有地方可以在ContentProvider查询中放置这样的子句。
有谁知道怎么做?我把mu代码放在下面:
private void getAllAlbumsFromSDCARD()
{
String[] cols = { MediaStore.Audio.Albums.ALBUM, MediaStore.Audio.Albums.ALBUM_ART, MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media._ID, MediaStore.Audio.Albums.NUMBER_OF_SONGS};
Uri allAlbumsUri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
albumsCursor = this.managedQuery(allAlbumsUri, cols, null, null, MediaStore.Audio.Media.ALBUM);
// SELECT IFNULL(title, "------") AS title FROM books;
Log.d(TAG, "" +albumsCursor.getCount());
if(albumsCursor!=null)
{
Log.d(TAG, "Managing cursor");
startManagingCursor(albumsCursor);
//
String[] from = new String[] { MediaStore.Audio.Albums.ALBUM_ART, MediaStore.Audio.Albums.ALBUM, MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Albums.NUMBER_OF_SONGS };
int[] to = new int[] { R.id.cover, R.id.album, R.id.artist, R.id.count };
ListAdapter albums = new SimpleCursorAdapter(this, R.layout.album_row, albumsCursor, from, to);
this.setListAdapter(albums);
}
}
答案 0 :(得分:0)
我发现我的问题的解决方案是自定义CursorAdapter。我粘贴了一个完全符合我想要的代码:
public class MyCursorAdapter extends SimpleCursorAdapter{
private MainActivity mainActivity;
private int layout;
public MyCursorAdapter(MainActivity context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.mainActivity = context;
this.layout = layout;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Cursor to current item
Cursor cursor = getCursor();
cursor.moveToPosition(position);
View v = mainActivity.getLayoutInflater().inflate(layout, parent, false);
TextView albumTV = (TextView) v.findViewById(R.id.album);
if (albumTV != null)
{
int index = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
String album = cursor.getString(index);
albumTV.setText(album);
}
TextView artistTV = (TextView)v.findViewById(R.id.artist);
if (artistTV != null)
{
int index = cursor.getColumnIndex(MediaStore.Audio.Albums.ARTIST);
String artist = cursor.getString(index);
artistTV.setText(artist);
}
TextView countTV = (TextView)v.findViewById(R.id.count);
if (countTV != null)
{
int index = cursor.getColumnIndex(MediaStore.Audio.Albums.NUMBER_OF_SONGS);
String count = cursor.getString(index);
countTV.setText(count);
}
ImageView cover = (ImageView) v.findViewById(R.id.cover);
if (cover != null)
{
int index = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);
String path = cursor.getString(index);
Log.d("MCA", ""+path);
if(path == null)
{
cover.setImageResource(R.drawable.no_cover_64);
cover.setScaleType(ImageView.ScaleType.FIT_CENTER);
}
else
{
BitmapDrawable icon = new BitmapDrawable(path);
cover.setImageDrawable(icon);
}
}
return v;
}
}