這篇文章將為大家詳細(xì)講解有關(guān)Android如何實(shí)現(xiàn)讀取SD卡下所有TXT文件名并用listView顯示出來的方法,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
“只有客戶發(fā)展了,才有我們的生存與發(fā)展!”這是創(chuàng)新互聯(lián)公司的服務(wù)宗旨!把網(wǎng)站當(dāng)作互聯(lián)網(wǎng)產(chǎn)品,產(chǎn)品思維更注重全局思維、需求分析和迭代思維,在網(wǎng)站建設(shè)中就是為了建設(shè)一個(gè)不僅審美在線,而且實(shí)用性極高的網(wǎng)站。創(chuàng)新互聯(lián)對成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站開發(fā)、網(wǎng)頁設(shè)計(jì)、網(wǎng)站優(yōu)化、網(wǎng)絡(luò)推廣、探索永無止境。
具體如下:
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如何實(shí)現(xiàn)讀取SD卡下所有TXT文件名并用listView顯示出來的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。
分享文章:Android如何實(shí)現(xiàn)讀取SD卡下所有TXT文件名并用listView顯示出來的方法
網(wǎng)站URL:http://www.rwnh.cn/article44/igpdhe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、定制開發(fā)、關(guān)鍵詞優(yōu)化、企業(yè)網(wǎng)站制作、微信小程序、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)