源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android实现读取SD卡下所有TXT文件名并用listView显示出来的方法

  • 时间:2022-04-28 10:55 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android实现读取SD卡下所有TXT文件名并用listView显示出来的方法
本文实例讲述了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 < 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相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/224.htm]Android编程开发之SD卡操作方法汇总[/url]》、《[url=http://www.1sucai.cn/Special/325.htm]Android文件操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/402.htm]Android数据库操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/367.htm]Android编程之activity操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/410.htm]Android开发入门与进阶教程[/url]》、《[url=http://www.1sucai.cn/Special/423.htm]Android资源操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/375.htm]Android视图View技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/124.htm]Android控件用法总结[/url]》 希望本文所述对大家Android程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部