Activity:
Java code
Package irdc. ex04_21;
/* Import related class */
Import java. io. File;
Import java. util. ArrayList;
Import java. util. List;
Import android. app. AlertDialog;
Import android. app. ListActivity;
Import android. content. DialogInterface;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. ArrayAdapter;
Import android. widget. ListView;
Import android. widget. TextView;
Public class EX04_21 extends ListActivity
{
/* Object Declaration
Items: stores the displayed name
Paths: file storage path
RootPath: Start directory
*/
Private List <String> items = null;
Private List <String> paths = null;
Private String rootPath = "/";
Private TextView mPath;
/** Called when the activity is first created .*/
@ Override
Protected void onCreate (Bundle icicle)
{
Super. onCreate (icicle );
/* Load main. xml Layout */
SetContentView (R. layout. main );
MPath = (TextView) findViewById (R. id. mPath );
GetFileDir (rootPath );
}
/* Method for obtaining the file architecture */
Private void getFileDir (String filePath)
{
/* Set the current path */
MPath. setText (filePath );
Items = new ArrayList <String> ();
Paths = new ArrayList <String> ();
File f = new File (filePath );
File [] files = f. listFiles ();
If (! FilePath. equals (rootPath ))
{
/* Set the first one to [return to the root directory] */
Items. add ("Back to" + rootPath );
Paths. add (rootPath );
/* Set the second stroke to [back to the upper layer] */
Items. add ("Back ../");
Paths. add (f. getParent ());
}
/* Add all files to the ArrayList */
For (int I = 0; I <files. length; I ++)
{
File file = files [I];
Items. add (file. getName ());
Paths. add (file. getPath ());
}
/* Declare the ArrayAdapter and use the Layout file file_row,
And set the Adapter to this ListActivity */
ArrayAdapter <String> fileList =
New ArrayAdapter <String> (this, R. layout. file_row, items );
SetListAdapter (fileList );
}
/* Set the action to be performed when the ListItem is pressed */
@ Override
Protected void onListItemClick (ListView l, View v, int position, long id)
{
File file = new File (paths. get (position ));
If (file. canRead ())
{
If (file. isDirectory ())
{
/* If it is a folder, read it again */
GetFileDir (paths. get (position ));
}
Else
{
/* If it is a file, AlertDialog will pop up */
New AlertDialog. Builder (this)
. SetTitle ("Message ")
. SetMessage ("[" + file. getName () + "] is File! ")
. SetPositiveButton ("OK ",
New DialogInterface. OnClickListener ()
{
Public void onClick (DialogInterface dialog, int which)
{
}
}). Show ();
}
}
Else
{
/* The displayed AlertDialog has insufficient permissions */
New AlertDialog. Builder (this)
. SetTitle ("Message ")
. SetMessage ("insufficient permissions! ")
. SetPositiveButton ("OK ",
New DialogInterface. OnClickListener ()
{
Public void onClick (DialogInterface dialog, int which)
{
}
}). Show ();
}
}
}
Layout:
File_row.xml
Java code
<? Xml version = "1.0" encoding = "UTF-8"?>
<TextView
Android: id = "@ + id/text1"
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "wrap_content"
Android: layout_height = "20px"
Android: textSize = "14sp"
/>
Main. xml
Java code
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: orientation = "vertical"
>
<TextView
Android: id = "@ + id/mPath"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: padding = "5px"
Android: textSize = "18sp"
Android: textColor = "@ drawable/blue"
/>
<ListView
Android: id = "@ android: id/list"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>
</LinearLayout>
Author: "asuschb"