读取SDCard下特定类型的文件,并添加响应,代码如下:
<span style="font-size:18px;"> private void getFileName(File[] files) {
if (files != null)// 先判断目录是否为空,否则会报空指针
{
for (File file : files) {
if (file.isDirectory()) {
getFileName(file.listFiles());
} else {
String fileName = file.getName();
if (fileName.endsWith(".xxx")) {
HashMap map = new HashMap();
String s = file</span>
getFileName函数读取xxx类型文件
<span style="font-size:18px;"> public void setPlayBack()
{
mListView = (ListView) findViewById(R.id.mListView);
name = new ArrayList();
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File path = Environment.getExternalStorageDirectory(); // 获得SD卡路径
File[] files = path.listFiles();// 读取
getFileName(files);//调用处理函数
}
SimpleAdapter adapter = new SimpleAdapter( //增加SimpleAdapter显示文件列表
MainActivity.this, name,
R.layout.sd_list,
new String[] { "Name" },
new int[] { R.id.tc_fileName });
mListView.setAdapter(adapter);
Log.i("MainActivity", "before mListView setOnClick");
mListView.setOnItemClickListener(new OnItemClickListener(){//对文件条目进行事件响应
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//do something
}
});
}</span>
setPlayBack函数将文件使用simpleAdapter以及listview进行显示,增加setOnItemClickListener监听,进行点击响应。