Android 带有光标和基本适配器的相册艺术未显示在列表或toast中

Android 带有光标和基本适配器的相册艺术未显示在列表或toast中,android,listview,cursor,baseadapter,albumart,Android,Listview,Cursor,Baseadapter,Albumart,带有光标和基本适配器的相册艺术未显示在列表或toast中 我试图在listview中显示相册艺术和相册详细信息,但它也不会在toast或listview中显示 这是密码 主要活动 public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstan

带有光标和基本适配器的相册艺术未显示在列表或toast中

我试图在listview中显示相册艺术和相册详细信息,但它也不会在toast或listview中显示

这是密码

主要活动

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //retrieveAudioFiles();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @SuppressLint("NewApi")
    public void retrieveAudioFiles(){
        SongsList songsList = new SongsList();

        //Uri sd = Audio.Media.INTERNAL_CONTENT_URI ;
        //Uri sd = Audio.Media.EXTERNAL_CONTENT_URI;
        Uri sd = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
        String[] cols = {Audio.Media.TITLE,Audio.Media.ARTIST,Audio.Media.ALBUM};
        String where = Audio.Media.IS_MUSIC;
        Cursor audioCursor = getContentResolver().query(sd,cols,where,null,null);

        while (audioCursor.moveToNext()){
            int posColTitle = audioCursor.getColumnIndex(Audio.Media.TITLE);
            int posColArtist = audioCursor.getColumnIndex(Audio.Media.ARTIST);
            int posColAlbum = audioCursor.getColumnIndex(Audio.Media.ALBUM);

            String songTitle = audioCursor.getString(posColTitle);
            String songArtist = audioCursor.getString(posColArtist);
            String songAlbum = audioCursor.getString(posColAlbum);
            int posColId = audioCursor.getColumnIndex(Audio.Media._ID);
            long songId = audioCursor.getLong(posColId);
            Uri songUri = ContentUris.withAppendedId(Audio.Media.EXTERNAL_CONTENT_URI,songId);
            String[] dataColumn = {Audio.Media.DATA};
            Cursor coverCursor = getContentResolver().query(songUri, dataColumn, null, null, null);
            coverCursor.moveToFirst();
            int dataIndex = coverCursor.getColumnIndex(Audio.Media.DATA);
            String filePath = coverCursor.getString(dataIndex);
            coverCursor.close();
            MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            retriever.setDataSource(filePath);
            byte[] coverBytes = retriever.getEmbeddedPicture();
            Bitmap songCover;
            Toast.makeText(getApplicationContext(),audioCursor.getString(audioCursor
                    .getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM)), 
                   Toast.LENGTH_LONG).show();

            if (coverBytes!=null) //se l'array di byte non è vuoto, crea una bitmap
                songCover = BitmapFactory.decodeByteArray(coverBytes, 0, coverBytes.length);
            else
                songCover=null;
            songsList.add(new Song(songTitle,songArtist,songAlbum));
            }
        audioCursor.close();
    }
    public class SongsAdapter extends BaseAdapter {

        private SongsList songsList;
        private LayoutInflater songInf;

        public SongsAdapter(Context c, SongsList theSongs){
            super();  
            songsList=theSongs;
            songInf=LayoutInflater.from(c);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return songsList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return songsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            RowWrapper wrapper;
            if (convertView == null)
            {
                convertView = songInf.inflate(
                    R.layout.song_row, null);
                wrapper = new RowWrapper(convertView);
                convertView.setTag(wrapper);
            }
            else
            {
                wrapper = (RowWrapper) convertView.getTag();
            }
            Song song = (Song) getItem(position);
            wrapper.populate(song);

            return convertView;
        }

        private class RowWrapper
        {
            private TextView titleTextView;
            private TextView artistTextView;
            private TextView albumTextView;
            private ImageView coverImageView;

            public RowWrapper(View convertView)
            {
                titleTextView = (TextView) convertView.findViewById(R.id.textTitle);
                artistTextView = (TextView) convertView.findViewById(R.id.textArtist);
                albumTextView = (TextView) convertView.findViewById(R.id.textAlbum);
                coverImageView = (ImageView) convertView.findViewById(R.id.smallCover);
            }

            public void populate(Song song)
            {
                titleTextView.setText(song.title);
                artistTextView.setText(song.artist);
                albumTextView.setText(song.album);
               if (song.cover != null)
                coverImageView.setImageBitmap(song.cover);
            }
        }

    }
}
然后在歌曲课上

public class Song {

    public String title="";
    public String artist="";
    public String album="";
    public Bitmap cover=null;

    public Song(String t, String ar, String al){
        title=t;
        artist=ar;
        album=al;
        //cover=c;
    }

    public Song(){

    }

}
和歌曲列表类

public class SongsList extends ArrayList<Song> {

    public SongsList(){
        super();
    }

}
公共类歌曲列表扩展了ArrayList{
公共歌曲列表(){
超级();
}
}
何况什么也没显示出来

下面是xml文件

主活动

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.stackalbumart.MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

    </ListView>

</RelativeLayout>

和song_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
    android:onClick="songPicked" >

    <ImageView
        android:id="@+id/smallCover"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:contentDescription="coverDescription"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/labelTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="labelTitle" />

        <TextView
            android:id="@+id/textTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:text="textTitle" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/labelArtist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:text="labelArtist" />

        <TextView
            android:id="@+id/textArtist"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:text="textArtist" />

        <TextView
            android:id="@+id/labelAlbum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="labelAlbum" />

        <TextView
            android:id="@+id/textAlbum"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:text="textAlbum" />
    </LinearLayout>

</LinearLayout>

应用程序刚刚打开,有一个空白的白色屏幕,甚至没有错误


真正应该做什么

既然您正在处理游标,我强烈建议您将
CursorAdapter
子类化,它扩展了
BaseAdapter
,并具有处理游标的附加逻辑

关于你的问题

RetrieveAudioles()
中,关闭光标后,应实例化适配器并将其提供给ListView。例如

ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(new SongsAdapter(this, songList));

你有
audioCursor
,为什么不使用
SimpleCursorAdapter
?我目前正在尝试学习android,这是我必须完成的任务,在相册艺术中使用光标和基本适配器如何在上面的代码中实现如果你想学习android,那么就使用
SimpleCorsorAdapter
,而不是编写数百行冗余代码…有没有一个例子我可以参考一下,SimpleCorsorAdapter是如何从要在listview中显示的mp3文件中显示相册艺术的关于如何使用
SimpleCursorAdapter
的示例,只需询问谷歌叔叔,正如你所说的,仍然没有显示任何内容检查
歌曲列表的内容,在while循环之后实际上他不需要
歌曲列表
all@abcd使用
DatabaseUtils.dumpCursor()
验证光标是否正确@pskink我同意您的意见。但实际上,SongsAdapter应该是一个
静态