温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-04-16 11:28:19 来源:亿速云 阅读:146 作者:小新 栏目:移动开发

这篇文章将为大家详细讲解有关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如何实现读取SD卡下所有TXT文件名并用listView显示出来的方法”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI