为什么这个游标适配器代码不起作用?

时间:2015-08-13 22:53:09

标签: android android-activity android-cursoradapter android-cursorloader

我想在点击网格中的相册时显示歌曲列表。

这里我的代码 DisplayAlbum扩展AppCompatActivity实现了LoaderManager.LoaderCallbacks

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.album_detail);
    Intent i = getIntent();
    b = i.getExtras();
    String str;
//    if((str =(String) b.getCharSequence("album_name"))!= null)
//        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
    val = new String[]{(String) b.getCharSequence("album_name")};


    ListView lv = (ListView) findViewById(R.id.al_songs);
    mAdapter = new DisplaySongsAdapter(this, null);
    lv.setAdapter(mAdapter);


}


static final String[] ALBUM_DETAIL_PROJECTION = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.ALBUM_ID,MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DURATION};
String where = MediaStore.Audio.Media.ALBUM + "=?";




public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String orderby = MediaStore.Audio.Media.TITLE;
    return new CursorLoader(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,ALBUM_DETAIL_PROJECTION, where, val, orderby);
}

这是 DisplaySongsAdapter扩展CursorAdapter

的代码
            public DisplaySongsAdapter(Context context, Cursor c) {
            super(context, c);
            mcontext=context;
            nInflater = LayoutInflater.from(context);
        }
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            TextView songTitle = (TextView) view.findViewById(R.id.songTitle);
            songTitle.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
            TextView alname = (TextView) view.findViewById(R.id.al_name);
            alname.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
            TextView artist = (TextView) view.findViewById(R.id.songArtist);
            artist.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));

            ImageView albumArt = (ImageView) view.findViewById(R.id.list_image);
            albumArt.setScaleType(ImageView.ScaleType.FIT_XY);
            ImageView albumimg = (ImageView) view.findViewById(R.id.al_art);
            albumimg.setScaleType(ImageView.ScaleType.FIT_XY);

            ImageButton listmenu = (ImageButton) view.findViewById(R.id.expanded_menu);
            listmenu.setOnClickListener(overflowClickListener);



            Long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));

            Bitmap img = getAlbumart(context,albumId);
            if(img != null) {
                albumArt.setImageBitmap(img);
                albumimg.setImageBitmap(img);
            }

            else{
                Bitmap def = getDefaultAlbumArt(context);
                albumArt.setImageBitmap(def);
                albumimg.setImageBitmap(img);
            }

        }


        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            final View view = nInflater.inflate(R.layout.song_item, parent, false);
            return view;
        }

但是活动并没有填充列表视图。它只是显示一个空白的膨胀布局。为什么会这样?我哪里出错了?

2 个答案:

答案 0 :(得分:0)

这可能与您实际上没有在适配器中提供任何项目有关: - )

mAdapter = new DisplaySongsAdapter(this, null);

答案 1 :(得分:0)

我使用SimpleCursorAdapter解决了它。但是我仍然想知道使用LoaderManager的方法