0

我正在为 Android 2.1 编写一个简单的音乐应用程序并偶然发现了这样一个问题:

在我的活动中,我有一个 ListView,其中包含专辑封面、专辑标题、艺术家和每行中的歌曲数量。我在 ContentProvider 中查询所有要放入 ListView 的值。但是 SDCARD 上的一些专辑没有专辑封面,我认为查询返回 NULL。结果在我的 ListView 中,我的专辑只有少数有封面。

我想做的是在查询 ContentProvider 时或在其他地方放置一些参数,如果查询返回 null 封面,则将我的默认图标作为专辑封面。我的意思是,如果 ContentProvider 没有返回任何内容作为封面,我想将我的默认图标放在这个地方。

也许我应该使用其他查询,而不是 ManagedCursor?

我看到了使用 IFNULL 子句(SELECT IFNULL(title, "------") 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);
    }
}
4

1 回答 1

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; 
}

}

于 2012-07-14T21:32:09.853 回答