This article describes the Android method of reading all TXT file names under the SD card and displaying them with listView. Share it for your reference, as follows:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class Txt_sdkaActivity extends Activity { private ListView lv; ArrayList name; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); lv = (ListView) findViewById(); name = new ArrayList(); if (().equals(Environment.MEDIA_MOUNTED)) { File path = ();// Obtain SD card path // File path = new File("/mnt/sdcard/"); File[] files = ();// Read getFileName(files); } SimpleAdapter adapter = new SimpleAdapter(this, name, , new String[] { "Name" }, new int[] { .txt_tv }); (adapter); for (int i = 0; i < (); i++) { ("zeng", "list. name: " + (i)); } } private void getFileName(File[] files) { if (files != null) {// First determine whether the directory is empty, otherwise a null pointer will be reported for (File file : files) { if (()) { ("zeng", "If it is a file directory. Continue reading 1" + ().toString() + ().toString()); getFileName(()); ("zeng", "If it is a file directory. Continue to read 2" + ().toString() + ().toString()); } else { String fileName = (); if ((".txt")) { HashMap map = new HashMap(); String s = (0, (".")).toString(); ("zeng", "File name txt::" + s); ("Name", fileName .substring(0, ("."))); (map); } } } } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android: android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ListView android: 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>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android: android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20pt" android:layout_weight="1" /> </LinearLayout>
For more information about Android related content, please check out the topic of this site:A summary of SD card operation methods for Android programming and development》、《Android file operation skills summary》、《Android database operation skills summary》、《Android programming activity operation skills summary》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.