你好,所以我真的是新的android和我开发的音乐播放器,到目前为止,我设法填充一个列表视图和所有把我需要按字母顺序排序的列表,我想在ImageView请帮助显示歌曲的专辑艺术。
Here is The Code
public class MainActivity extends Activity {
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init_phone_music_grid();
}
private void init_phone_music_grid() {
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Albums.ALBUM_ID,
MediaStore.Audio.Albums._ID,
MediaStore.Audio.Albums.ALBUM_ART,
MediaStore.Video.Media.SIZE };
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null, null);
count = musiccursor.getCount();
musiclist = (ListView) findViewById(R.id.listView);
musiclist.setAdapter(new MusicAdapter(getApplicationContext()));
musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();
}
private OnItemClickListener musicgridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,long id) {
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
String filename = musiccursor.getString(music_column_index);
try {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(filename);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
}
}
};
public class MusicAdapter extends BaseAdapter {
private Context mContext;
public MusicAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String id = null;
String id1 = null;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
musiccursor.moveToPosition(position);
id1 = musiccursor.getString(music_column_index);
TextView Title = (TextView) rowView.findViewById(R.id.titlelist);
TextView Artist = (TextView) rowView.findViewById(R.id.artistlist);
ImageView imageView = (ImageView) rowView.findViewById(R.id.iconlist);
// What should i do to set the album art to this imageview ????
Title.setText(id);
Artist.setText(id1);
// change the icon for Windows and iPhone
return rowView;
}
}
}
要获得排序的歌曲列表,将managedQuery的最后一个参数从null更改为Attribute,您希望对结果进行排序
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null,COLUMN_NAME);
。如果你想对歌曲的标题列表进行排序请输入
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null,MediaStore.Audio.Media.TITLE);
看看这个图书馆。它有助于按字母顺序划分列表,类似于IOS。