专辑列表就是显示歌曲所属的专辑,也是用ListView来显示,同样也需要定义一个Adapter。
一、Activity
/**
*
*/
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());
}
class AlbumsItemClickListener implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
Intent intent = new Intent();
intent.setClass(AlbumActivity.this, SongsOfAlbumActivity.class);
intent.putExtra("albums", albums[position]);
startActivity(intent);
}
}
}
二、布局
1、albums.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/albums_listview_id"
android:background="@drawable/listbg"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
2、albumslist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dip"
android:background="@android:color/transparent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/Albumsitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingRight="5dip" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/album"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#ffffff"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:id="@+id/mysinger"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="5dip"
android:textColor="#ffffff"
android:textSize="13dp" />
</LinearLayout>
</LinearLayout>
三、适配器Adapter
/**
*
*/
package com.android.musicplayer.adapter;
import java.util.HashMap;
import com.android.musicplayer.activity.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* @author Administrator
*
*/
public class AlbumListAdapter extends BaseAdapter {
private Context myContext;
private String[] albums;
private HashMap<String, String> myMap;
public AlbumListAdapter(Context context, String[] str1,
HashMap<String, String> map) {
myContext = context;
albums = str1;
myMap = map;
}
public int getCount() {
return albums.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
View vi = convertView;
if (convertView == null) {
holder = new ViewHolder();
vi = LayoutInflater.from(myContext).inflate(R.layout.albumslist,
null);
holder.album = (TextView) vi.findViewById(R.id.album);
holder.artist = (TextView) vi.findViewById(R.id.mysinger);
holder.Albumsitem = (ImageView) vi.findViewById(R.id.Albumsitem);
vi.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (albums[position].length() > 15) {// 设置专辑名
holder.album.setText(albums[position].substring(0, 12) + "...");
} else {
holder.album.setText(albums[position]);
}
if (albums[position].equals("sdcard")) {// 设置艺术家姓名
holder.artist.setText("未知艺术家");
} else {
holder.artist.setText(myMap.get(albums[position]));
}
holder.Albumsitem.setImageResource(R.drawable.album);
return vi;
}
class ViewHolder {
public TextView album, artist;
public ImageView Albumsitem;
}
}
三、效果
这就是专辑列表,点击去进后会看到下面的歌曲。