Android實(shí)現(xiàn)讀取SD卡下所有TXT文件名并用listView顯示出來的方法
本文實(shí)例講述了Android實(shí)現(xiàn)讀取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", "若是文件目錄。繼續(xù)讀1" + file.getName().toString() + file.getPath().toString()); getFileName(file.listFiles()); Log.i("zeng", "若是文件目錄。繼續(xù)讀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>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android編程開發(fā)之SD卡操作方法匯總》、《Android文件操作技巧匯總》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
詳解Android 全局彈出對話框SYSTEM_ALERT_WINDOW權(quán)限
本篇文章主要介紹了詳解Android 全局彈出對話框SYSTEM_ALERT_WINDOW權(quán)限,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03AndroidStudio項(xiàng)目打包成jar的簡單方法
JAR(Java Archive,Java 歸檔文件)是與平臺無關(guān)的文件格式,它允許將許多文件組合成一個壓縮文件,在eclipse中我們知道如何將一個項(xiàng)目導(dǎo)出為jar包,供其它項(xiàng)目使用呢?下面通過本文給大家介紹ndroidStudio項(xiàng)目打包成jar的簡單方法,需要的朋友參考下吧2017-11-11Android開發(fā)新手必須知道的10大嚴(yán)重錯誤
這篇文章主要介紹了Android開發(fā)新手必須知道的10大嚴(yán)重錯誤,總結(jié)分析了Android開發(fā)中幫助文件、開發(fā)工具、社區(qū)等的重要性以及重要的開發(fā)原則,需要的朋友可以參考下2016-01-01Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析
StrictMode意思為嚴(yán)格模式,是用來檢測程序中違例情況的開發(fā)者工具。最常用的場景就是檢測主線程中本地磁盤和網(wǎng)絡(luò)讀寫等耗時的操作。這篇文章給大家介紹Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析,感興趣的朋友一起看看吧2018-01-01Android ListView 子控件onClick正確獲取position的方法
這篇文章主要介紹了Android ListView 子控件onClick正確獲取position的方法,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧2017-01-01Android Studio多工程引用同一個library項(xiàng)目配置的解決方法
大家在使用android studio的時候,會遇到多個項(xiàng)目引用相同的library這篇文章主要介紹了Android Studio多工程引用同一個library項(xiàng)目配置方法,需要的朋友可以參考下2018-03-03Android注冊登錄實(shí)時自動獲取短信驗(yàn)證碼
注冊登錄或修改密碼功能常常需要輸入短信驗(yàn)證碼,如何自動獲取短信驗(yàn)證碼,這篇文章就為大家介紹了Androidcv注冊登錄自動獲取短信驗(yàn)證碼的實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-08-08