main.xmljava
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android :color/holo_blue_light"
tools:context="com.example.mymusiclist.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="21sp"
android:text="音樂列表" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
>
</ListView>
</RelativeLayout>android
listview.xml(佈局xml)app
<?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" >
<TextView
android:id="@+id/music_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:text="music_title" />
<LinearLayout
android:id="@+id/ll1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:orientation="horizontal" >
<TextView
android:id="@+id/music_artist"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="3dp"
android:text="music_artist" />
<TextView
android:id="@+id/music_duration"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="music_duration" />
</LinearLayout>
</LinearLayout>ide
MainActivity.java佈局
package com.example.mymusiclist;
import java.util.ArrayList;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.provider.MediaStore;
public class MainActivity extends ActionBarActivity {
private ListView listView;
private ArrayList<MusicMp3> musicList = new ArrayList<MusicMp3>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
listView = (ListView) findViewById(R.id.listView1);
musicList = getMp3Infos();
MusicListAdapter adapter = new MusicListAdapter(this, musicList);
listView.setAdapter(adapter);
}
public ArrayList<MusicMp3> getMp3Infos() { //獲取音樂列表信息
Cursor cursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
ArrayList<MusicMp3> mp3Infos = new ArrayList<MusicMp3>();
for (int i = 0; i < cursor.getCount(); i++) {
MusicMp3 mp3Info = new MusicMp3();
cursor.moveToNext();
long id = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media._ID)); //音樂id
String title = cursor.getString((cursor
.getColumnIndex(MediaStore.Audio.Media.TITLE)));//音樂標題
String artist = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST));//藝術家
long duration = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media.DURATION));//時長
long size = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media.SIZE)); //文件大小
String url = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA)); //文件路徑
int isMusic = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//是否爲音樂
if (isMusic != 0) { //只把音樂添加到集合當中
mp3Info.setId(id);
mp3Info.setTitle(title);
mp3Info.setArtist(artist);
mp3Info.setDuration(duration);
mp3Info.setSize(size);
mp3Info.setUrl(url);
mp3Infos.add(mp3Info);
}
}
return mp3Infos;
}
}this
MusicListAdapter.javaurl
package com.example.mymusiclist;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MusicListAdapter extends BaseAdapter {
private final static String TAG = "MusicListAdapter";
private LayoutInflater inflater;
private Context mContext;
private ArrayList<Mp3Info> mList;
public MusicListAdapter(Context context, ArrayList<Mp3Info> list){
mContext = context;
mList = list;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view;
if(convertView != null){
view = convertView;
} else {
view = inflater.inflate(R.layout.listview_item, parent, false);
}
TextView title = (TextView)view.findViewById(R.id.music_title);
TextView artist = (TextView) view.findViewById(R.id.music_artist);
TextView duration = (TextView) view.findViewById(R.id.music_duration);
String music_title = mList.get(position).getTitle();
String music_artist = mList.get(position).getArtist();
long music_duration = mList.get(position).getDuration();
title.setText(music_title);
if(music_artist.equals("<unknown>")){
artist.setText("未知藝術家");
}else{
artist.setText(music_artist);
}
Log.d(TAG, "music_artist============="+music_artist);
duration.setText(getStringTime(music_duration));
return view;
}
public String getStringTime(long duration){
SimpleDateFormat sDateFormat = new SimpleDateFormat("mm:ss");
return sDateFormat.format(new Date(duration));
}
}.net
屬性實體MusicMp3.java3d
package com.example.mymusiclist;
public class MusicMp3 {
//屬性值,生成get和set方法 構造方法
long id;
long duration;
long size;
String title;
String url;
String artist;
public MusicMp3(){ }
public MusicMp3(long id, long duration, long size, String title, String url,
String artist) {
super();
this.id = id;
this.duration = duration;
this.size = size;
this.title = title;
this.url = url;
this.artist = artist;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
@Override
public String toString() {
return "Mp3Info [id=" + id + ", duration=" + duration + ", size="
+ size + ", title=" + title + ", url=" + url + ", artist="
+ artist + "]";
}
}
orm