您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Android如何實(shí)現(xiàn)讀取SD卡下所有TXT文件名并用listView顯示出來(lái)的方法,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
具體如下:
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顯示出來(lái)的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。