主函数
package com.example.mylistview;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void toLove1(View v){
startActivity(new Intent(this,MySong1_Activity.class));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MySong1_Activity
package com.example.mylistview;
import java.util.ArrayList;
import java.util.List;
import com.example.adapter.List1_adapter;
import com.example.bean.MusicBean;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ListView;
public class MySong1_Activity extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list1);
listView = (ListView) this.findViewById(R.id.list1);
List<MusicBean> listMusic = MySong1_Activity
.showList1(getApplicationContext());
List1_adapter adapter = new List1_adapter(this, listMusic);
listView.setAdapter(adapter);
}
public static List<MusicBean> showList1(Context context) {
List<MusicBean> musicList = new ArrayList<MusicBean>();
if (context.getContentResolver() != null) {
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,
null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
if (null == cursor) {
return null;
}
if (cursor.moveToFirst()) {
do {
MusicBean m = new MusicBean();
String title = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.TITLE));
String singer = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST));
if ("<unknown>".equals(singer)) {
singer = "未知艺术家";
}
long size = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media.SIZE));
long time = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media.DURATION));
String path = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
String name = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String sbr = name.substring(name.length() - 3,
name.length());
if (sbr.equals("mp3")) {
m.setTitle(title);
m.setSinger(singer);
m.setSize(size);
m.setTime(time);
m.setPath(path);
m.setName(name);
musicList.add(m);
}
} while (cursor.moveToNext());
}
}
return musicList;
}
}
MusicBean
package com.example.bean;
public class MusicBean {
private String title;
private String singer;
private String path;
private long size;
private long time;
private String name;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
List1_adapter
package com.example.adapter;
import java.util.List;