Android读取SD卡下面所有的TXT文件名 listView显示出来
2013-05-18 01:50
507 查看
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 < name.size(); i++) { Log.i("zeng", "list. name: " + name.get(i)); } } private void getFileName(File[] files) { if (files != null) {// 先判断目录是否为空,否则会报空指针 for (File file : files) { if (file.isDirectory()) { Log.i("zeng", "若是文件目录。继续读1" + file.getName().toString() + file.getPath().toString()); getFileName(file.listFiles()); Log.i("zeng", "若是文件目录。继续读2" + file.getName().toString() + file.getPath().toString()); } else { String fileName = file.getName(); if (fileName.endsWith(".txt")) { HashMap map = new HashMap(); String s = fileName.substring(0, fileName.lastIndexOf(".")).toString(); Log.i("zeng", "文件名txt:: " + s); map.put("Name", fileName .substring(0, fileName.lastIndexOf("."))); name.add(map); } } } } } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginTop="62dp" > </ListView> </RelativeLayout>
pes.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/txt_tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20pt" android:layout_weight="1" /> </LinearLayout>
相关文章推荐
- Android读取SD卡下面所有的TXT文件名 listView显示出来
- android读取SD卡下面所有的TXT文件名,listView显示出来。
- Android实现读取SD卡下所有TXT文件名并用listView显示出来的方法
- 今天学习了怎么读取Android数据库读取数据,用ListView将其显示出来
- Android中读取SD卡中的图片,并显示到ListView
- android:读取SD卡中的图片显示出来(缩略图),SD卡工具类(13)
- Android之遍历SD卡所有文件显示在ListView
- 在android中用NetworkImageView读取网络图片并通过ListView显示出来
- 使用VC实现读取指定路径下的文件,并显示所有文件的文件名和文件图标
- android通过ContentPrivider扫描sd卡所有图片,并显示
- Android进阶之读取手机联系人显示listview并点击拨号(CursorAdapter)
- android---读取SD卡内容并显示
- Android读取SDCard文件到ListView显示
- Android 读取sd卡图片并显示
- android 根据SD卡中图片路径读取并显示SD中的图片——源代码
- 读取所有的android应用显示在Item中,按键有响应
- Android 的ListView操作实例,文件管理器SD卡的读取操作
- Android高德地图如何让所有的轨迹在屏幕范围内都显示出来.
- android学习笔记(一) 遍历SD卡中所有文件并在listview中显示
- Python实现读取目录所有文件的文件名并保存到txt文件代码