Android用ListView显示SDCard文件列表的小例子
程序员文章站
2022-07-11 19:35:26
复制代码 代码如下:filepath = environment.getexternalstoragedirectory().getabsolutepath() + "/a...
复制代码 代码如下:
filepath = environment.getexternalstoragedirectory().getabsolutepath() + "/adasitemaps/sigrecord";
file file=new file(filepath);
file[] files = file.listfiles();
构造adapter,
复制代码 代码如下:
for(file mcurrentfile:files){
mfilename.add(mcurrentfile.getname());
}
arrayadapter<string> madapter = new arrayadapter<string>(this, android.r.layout.simple_expandable_list_item_1,mfilename);
显示 listview
复制代码 代码如下:
filelistview.setadapter(madapter);
listview点击事件的处理:
复制代码 代码如下:
filelistview = (listview) findviewbyid(r.id.filelistview);
filelistview.setonitemclicklistener(filelistviewitemclicklistener);
private adapterview.onitemclicklistener filelistviewitemclicklistener =
new adapterview.onitemclicklistener(){
@override
public void onitemclick(adapterview<?> parent, view view, int position,
long id) {
// todo auto-generated method stub
//test.........................
// textview textview = (textview)findviewbyid(r.id.textviewfile);
// textview.append(mfilename.get(position).tostring());
}
};
结果如下图:
上一篇: 黑枸杞和茶能够一起泡吗