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) {// 先判斷目錄是否為空,否則會(huì)報(bào)空指針
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é)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android設(shè)計(jì)模式之Builder模式解析
這篇文章主要為大家詳細(xì)介紹了Android設(shè)計(jì)模式之Builder模式解析的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android自定義View 實(shí)現(xiàn)水波紋動(dòng)畫引導(dǎo)效果
在android程序開發(fā)中,我們經(jīng)常簡(jiǎn)單通過自定義view實(shí)現(xiàn)水波紋動(dòng)畫引導(dǎo)功能,下面通過本文給大家分享實(shí)現(xiàn)代碼,需要的朋友參考下2017-01-01
Android使用Service實(shí)現(xiàn)簡(jiǎn)單音樂播放實(shí)例
這篇文章主要為大家詳細(xì)介紹了Android使用Service實(shí)現(xiàn)簡(jiǎn)單音樂播放實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android Jetpack組件中LiveData的優(yōu)劣
LiveData是Jetpack組件的一部分,更多的時(shí)候是搭配ViewModel來使用,相對(duì)于Observable,LiveData的最大優(yōu)勢(shì)是其具有生命感知的,換句話說,LiveData可以保證只有在組件(?Activity、Fragment、Service)處于活動(dòng)生命周期狀態(tài)的時(shí)候才會(huì)更新數(shù)據(jù)2023-04-04
Flutter?LinearProgressIndicator使用指南分析
這篇文章主要為大家介紹了Flutter?LinearProgressIndicator使用指南分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
簡(jiǎn)單實(shí)現(xiàn)android輪播圖
這篇文章主要為大家詳細(xì)介紹了android輪播圖的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

