本文实例讲述了Android实现读取SD卡下所有TXT文件名并用listView显示出来的方法。分享给大家供大家参考,具体如下:
MainActivity.Java
package com.zxl; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.widget.ListView; import android.widget.SimpleAdapter; public class Txt_sdkaActivity extends Activity { private ListView lv; ArrayList name; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lv = (ListView) findViewById(R.id.lv); name = new ArrayList(); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File path = Environment.getExternalStorageDirectory();// 获得SD卡路径 // File path = new File("/mnt/sdcard/"); File[] files = path.listFiles();// 读取 getFileName(files); } SimpleAdapter adapter = new SimpleAdapter(this, name, R.layout.pes, new String[] { "Name" }, new int[] { R.id.txt_tv }); lv.setAdapter(adapter); for (int i = 0; i
main.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
pes.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android编程开发之SD卡操作方法汇总》、《Android文件操作技巧汇总》、《Android数据库操作技巧总结》、《Android编程之activity操作技巧总结》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。