專輯列表就是顯示歌曲所屬的專輯,也是用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());
}
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);
}
}
}
/**
*
*/
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
[java]
<?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>
<?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
[java]
<?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>
<?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
[java] view plaincopyprint?/**
*
*/
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;
}
}
/**
*
*/
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 { www.2cto.com
holder.artist.setText(myMap.get(albums[position]));
}
holder.Albumsitem.setImageResource(R.drawable.album);
return vi;
}
class ViewHolder {
public TextView album, artist;
public ImageView Albumsitem;
}
}