国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現讀取SD卡下所有TXT文件名并用listView顯示出來的方法

2019-10-22 18:33:24
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 柏乡县| 民权县| 新源县| 淄博市| 天峻县| 玉树县| 鄄城县| 景宁| 辛集市| 临高县| 旺苍县| 阿拉善盟| 赤壁市| 罗山县| 云梦县| 额济纳旗| 榆树市| 稷山县| 澄迈县| 四子王旗| 凤阳县| 威宁| 莱芜市| 翼城县| 虹口区| 红河县| 金华市| 邵阳市| 洛川县| 富阳市| 塔河县| 南康市| 新泰市| 宜章县| 马龙县| 天祝| 太仓市| 漠河县| 揭东县| 呼玛县| 遂平县|