music_item.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="fill_parent"
android:orientation="horizontal"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="1dip"
android:paddingBottom="1dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mp3_item"
/>
<TextView android:id="@+id/mp3_name"
android:layout_height="30dip"
android:layout_width="fill_parent"
android:textSize="10pt"
android:paddingLeft="10dp"
android:textColor="#FFFFFF"/>
</LinearLayout>
列表
/**
* 文件过滤器
*
* @author
*
*/
class MusicFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String filename) {
return (filename.endsWith(".mp3"));
}
}
//绑定音乐
void musicList(){
try{
musiclist.clear();
//扫描SD卡下MP3文件夹中的Mp3歌曲,将扫描出的MP3对象存放到集合中
File mp3File=new File(SkyMusicActivity.SDPath);
if(mp3File.listFiles(new MusicFilter()).length>=0){
for(File file:mp3File.listFiles(new MusicFilter())){
Mp3 mp3=new Mp3();
String[] str=file.getName().split("\\.");
mp3.setMp3_name(str[0]);
mp3.setId(i);
i++;
musiclist.add(mp3);
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
HashMap<String, String> map=null;
List<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>();
if(musiclist.size()!=0){
for(Mp3 m:musiclist){
map=new HashMap<String, String>();
map.put("mp3_item", Integer.toString(m.getId()+1));
map.put("mp3_name", m.getMp3_name());
list.add(map);
}
SimpleAdapter adapter=new SimpleAdapter
(this, list, R.layout.music_item, new String[]{"mp3_item","mp3_name"}, new int[]{R.id.mp3_item,R.id.mp3_name});
setListAdapter(adapter);
}
}
实现后的界面