你好,游客 登录 注册 搜索
背景:
阅读新闻

android音乐播放器_专辑下歌曲列表 - wangjinyu501的专栏

[日期:2013-03-19] 来源:  作者: [字体: ]

    点击专辑列表就进入了这个专辑下的歌曲列表。在这里也是用一个ListView作为显示的工具,当然也可以用其他的组件。

一、Activity 

/**
 * 
 */
package com.android.musicplayer.activity;

import java.io.File;
import com.android.musicplayer.adapter.MusicListAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;

/**
 * @author Administrator
 * 
 */

public class SongsOfAlbumActivity extends Activity {

	private int[] _ids;
	private String[] _titles;
	private String[] _artists;
	private String[] _path; // 音乐文件的路径
	private ListView listview;
	private int pos;
	private String albumName;
	private MusicListAdapter adapter;
	/* 上下文菜单项 */
	private static final int PLAY_ITEM = Menu.FIRST;
	private static final int DELETE_ITEM = Menu.FIRST + 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = this.getIntent();
		albumName = intent.getExtras().getString("albums");//获取被点击的专辑名,方便查询此专辑下的歌曲信息
		listview = new ListView(this);
		setListData();
		LinearLayout list = new LinearLayout(this);
		list.setBackgroundResource(R.drawable.listbg);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		list.addView(listview, params);
		setContentView(list);
	}

	private void setListData() {
		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._ID,
						MediaStore.Audio.Media.DISPLAY_NAME,
						MediaStore.Audio.Media.DATA,
						MediaStore.Audio.Media.ALBUM_ID, },
				MediaStore.Audio.Media.ALBUM + "='" + albumName + "'",

				null, null);
		c.moveToFirst();
		_ids = new int[c.getCount()];
		_titles = new String[c.getCount()];
		_path = new String[c.getCount()];
		_artists = new String[c.getCount()];
		for (int i = 0; i < c.getCount(); i++) {
			_ids[i] = c.getInt(3);
			_titles[i] = c.getString(0);
			_path[i] = c.getString(5);
			_artists[i] = c.getString(c
					.getColumnIndex(MediaStore.Audio.Media.ARTIST));
			c.moveToNext();
		}
		adapter = new MusicListAdapter(this, c);
		listview.setAdapter(adapter);
	}
}

    理解之前的歌曲列表,这里就比较简单了。只要获取了专辑的信息,albumName = intent.getExtras().getString("albums");再用Cursor c = this.getContentResolver().query()进行查询就可以获取这个专辑下的歌曲信息,然后再显示出来就ok了。

                                                     

  





收藏 推荐 打印 | 录入:admin | 阅读:
相关新闻