listView 结合 ArrayList和HashMap 的应用

 

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);
}
}


实现后的界面

转载于:https://www.cnblogs.com/shanshan520/archive/2012/03/11/2390541.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值