2012-07-14 58 views
0

我正在写一个简单的音乐应用程序为Android 2.1和偶然发现了这样一个问题:如何将默认值在查询到Android的ContentProvider

在我的活动我有一个ListView,里面有专辑封面,专辑名称,艺术家和每行歌曲的数量。我查询ContentProvider的所有这些值放在ListView中。但是SDCARD上的一些专辑没有专辑封面,查询返回NULL我认为。结果在我的ListView中,我拥有只有少数人拥有封面的相册。

我想要做的是在查询ContentProvider或其他位置时放置一些参数,以便将默认图标设置为相册封面,如果查询返回空覆盖。我的意思是,如果ContentProvider没有返回任何封面,我想把我的默认图标放在这个地方。

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

我看到了使用IFNULL子句(SELECT IFNULL(title,“------”)AS title FROM books;)的类似db问题的解决方案。我认为它可以以某种方式提供帮助,但是在ContentProvider查询中没有放置这样的子句的地方。

有谁知道该怎么做?我把以下的亩代码:

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

我发现我的问题的解决方案是自定义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; 
} 

}

相关问题