专辑列表就是显示歌曲所属的专辑,也是用ListView来显示,同样也需要定义一个Adapter。
一、Activity
[java]
/**
*
*/
package com.android.musicplayer.activity;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import com.android.musicplayer.adapter.AlbumListAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
/**
* @author Administrator
*
*/
public class AlbumActivity extends Activity {
private String[] albums;
private ListView listView;
private AlertDialog ad = null;
private AlertDialog.Builder builder = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.albums);
setListData();
}
private void setListData() {
listView = (ListView) findViewById(R.id.albums_listview_id);
listView.setCacheColorHint(00000000);// 设置这个就不会出现ListView滑动画面混乱的情况。
Cursor c = this.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME }, null, null,
MediaStore.Audio.Media.ALBUM);
if (c == null || c.getCount() == 0) {
builder = new AlertDialog.Builder(this);
builder.setMessage("存储列表为空...").setPositiveButton("确定", null);
ad = builder.create();
ad.show();
return;
}
c.moveToFirst();
int num = c.getCount();
HashSet<String> set = new HashSet<String>();
for (int i = 0; i < num; i++) {
String szAlbum = c.getString(c
.getColumnIndex(MediaStore.Audio.Media.ALBUM));
set.add(szAlbum);
c.moveToNext();
}
num = set.size();
Iterator<String> it = set.iterator();
albums = new String[num];
int i = 0;
while (it.hasNext()) {
albums[i] = it.next().toString();
i++;
}
String album = "";
for (int j = 0; j < num; j++) {
if (j < num - 1) {
album = album + "'" + albums[j] + "',";
} else {
album = album + "'" + albums[j] + "'";
}
}
Cursor c1 = this.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME, }, null, null,
MediaStore.Audio.Media.ALBUM);
c1.moveToFirst();
HashMap<String, String> map = new HashMap<String, String>();
int num1 = c1.getCount();
for (int j = 0; j < num1; j++) {
map.put(c1.getString(3), c1.getString(2));
c1.moveToNext();
}
listView.setAdapter(new AlbumListAdapter(this, albums, map));
listView.setOnItemClickListener(new AlbumsItemClickListener());