如何使用adapter 填充ListView

使用ListView 需要使Activity 继承ListActivity

生成以和 list 的List<hashMap<>>。将数据反复到list中。生成一个adapter 然后调用SetAdapter 。界面会自动渲染。

public class LocalMp3ListActivity extends ListActivity {


private List<Mp3Info> mp3Infos=null;
private static final String path ="mp3";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_mp3_list);

}

@Override
protected void onResume() {
updateListView();
super.onResume();
}

public void updateListView()
{
FileUtils fileUtils= new FileUtils();
mp3Infos=fileUtils.getMp3files(path);
setListAdapter(buildSimpleAdapter( mp3Infos));

}

private SimpleAdapter buildSimpleAdapter(List<Mp3Info> mp3Infos)
{
List<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();
for(Iterator iterator=mp3Infos.iterator();iterator.hasNext();)
{
Mp3Info mp3Info = (Mp3Info) iterator.next();
HashMap<String,String> map=new HashMap<String,String>();
map.put("mp3_name", mp3Info.getMp3Name());
map.put("mp3_size", mp3Info.getMp3Size());
list.add(map); 
}
 
SimpleAdapter simpleAdapter =new SimpleAdapter(
this,list,R.layout.mp3info_item,
new String[]{"mp3_name","mp3_size"}, 
new int [] {R.id.mp3_name,R.id.mp3_size});
return simpleAdapter;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值