Only one week after learning Android, I saw some examples in some documents. There are quite a few scientific things in the implementation of file resource manager.
Code in filemanageractivity on the homepage:
Package COM. hoperun. activity; </P> <p> Import Java. io. file; <br/> Import Java. util. arraylist; </P> <p> Import android. app. alertdialog; <br/> Import android. app. listactivity; <br/> Import android. content. dialoginterface; <br/> Import android. content. intent; <br/> Import android. content. dialoginterface. onclicklistener; <br/> Import android.net. uri; <br/> Import android. OS. bundle; <br/> Import android. view. layoutin Flater; <br/> Import android. view. view; <br/> Import android. widget. edittext; <br/> Import android. widget. listview; <br/> Import android. widget. textview; </P> <p> Import COM. hoperun. adapter. filelistadapter; </P> <p> public class filemanageractivity extends listactivity {<br/>/** called when the activity is first created. */<br/> private textview showxpath; // display the file path <br/> private arraylist <string> items ;/ /Name of the file to be displayed <br/> private arraylist <string> paths; // display the file path <br/> private string rootpath = "/"; // root directory <br/> private view renamedialogview; // rename the dialog box view <br/> private edittext nameedit; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> showxpath = (textview) findviewbyid (R. id. XPath); <br/> getfiled Ir (rootpath); // obtain the file list <br/>}</P> <p> // method for obtaining the file list <br/> private void getfiledir (string path) {<br/> showxpath. settext (PATH); // display the current path <br/> items = new arraylist <string> (); <br/> paths = new arraylist <string> (); <br/> // obtain the file in the current path <br/> file presentfile = new file (PATH); <br/> file [] files = presentfile. listfiles (); </P> <p> If (! Path. equals (rootpath) {<br/> // returns the root directory <br/> items. add ("back to/"); <br/> paths. add (rootpath); <br/> // return to the upper-level directory <br/> items. add ("back previous"); <br/> paths. add (presentfile. getparent (); <br/>}</P> <p> // Add all file names and paths in the current path <br/> for (file F: files) {<br/> items. add (F. getname (); <br/> paths. add (F. getpath (); <br/>}</P> <p> // set the list adapter <br/> setlistadapter (New filelistadapter (filemanageractivity. this, items, Paths )); <br/>}</P> <p> // Click Event of the item in the list <br/> @ override <br/> protected void onlistitemclick (listview L, view V, int position, long ID) {<br/> file F = new file (paths. get (position); <br/> If (F. isdirectory () {<br/> getfiledir (paths. get (position); <br/>}else {<br/> filehandle (f ); <br/>}</P> <p> // file object processing method <br/> private void filehandle (final file F) {<br/> // sets the listener operation. When you click the list file item, a dialog box is displayed. <br /> Onclicklistener clicklistener = new dialoginterface. onclicklistener () {</P> <p> Public void onclick (dialoginterface dialog, int which) {<br/> If (which = 0) {// open the file <br/> openfile (f); <br/>} else if (which = 1) {// Modify file name <br/> // create and modify file name dialog box <br/> layoutinflater renameinflater = layoutinflater <br/>. from (filemanageractivity. this); <br/> renamedialogview = renameinflater. inflate (<br/> r. layout. rename _ Alert_dialog, null); <br/> nameedit = (edittext) renamedialogview. findviewbyid (R. id. nameedit); <br/> // set that the content of the edit box has no file name <br/> nameedit. settext (F. getname (); </P> <p> // select the operation to be performed in the Options dialog box, listener <br/> onclicklistener renamedialoglistener = new dialoginterface. onclicklistener () {</P> <p> Public void onclick (dialoginterface dialog, int which) {<br/> string nameeditstr = nameedit. gettext (). tostring (); // get Rename the text content in the edit box <br/> final string postrenamedfilepath = f. getparentfile (). getpath () <br/> + "/" + nameeditstr; // The renamed file path </P> <p> If (new file (postrenamedfilepath ). exists () {// The modified file name conflicts with an existing file name. <br/> // If (! Nameeditstr. equals (F. getname () {<br/> New alertdialog. builder (filemanageractivity. this) <br/>. settitle ("prompt "). setmessage ("the file already exists. Do you want to overwrite it? ") <Br/>. setpositivebutton ("OK", new dialoginterface. onclicklistener () {<br/> // confirm the operation to overwrite the original file <br/> Public void onclick (dialoginterface dialog, int which) {<br/> F. renameto (new file (postrenamedfilepath); // rename overwrite file <br/> getfiledir (F. getparent (); // lists the renamed results <br/>}</P> <p> }). setnegativebutton ("cancel", new dialoginterface. onclicklistener () {</P> <p> Public void onclick (<br/> dialoginterface dialog, <br/> int Which) {<br/>}</P> <p> }). show (); <br/>}</P> <p >}else {<br/> F. renameto (new file (postrenamedfilepath); // rename overwrite file <br/> getfiledir (F. getparent (); // lists the renamed results <br/>}< br/> }; </P> <p> // rename dialog box <br/> alertdialog renamedialog = new alertdialog. builder (filemanageractivity. this ). create (); <br/> renamedialog. setview (renamedialogview); // attempts to set the rename dialog box </P> <p> renamedialog. setbutton ("OK", renamedialoglisten Er); <br/> renamedialog. setbutton2 ("cancel", new dialoginterface. onclicklistener () {</P> <p> Public void onclick (dialoginterface dialog, int which) {<br/>}</P> <p> }); <br/> renamedialog. show (); </P> <p >}else {<br/> // Delete the selected file option <br/> New alertdialog. builder (filemanageractivity. this ). settitle ("delete") <br/>. setmessage ("are you sure you want to delete this file? "). Setpositivebutton ("OK", new dialoginterface. onclicklistener () {<br/> // confirm to delete the file <br/> Public void onclick (dialoginterface dialog, <br/> int which) {<br/> F. delete (); // delete a file <br/> getfiledir (F. getparent (); <br/>}</P> <p> }). setnegativebutton ("cancel", new dialoginterface. onclicklistener () {</P> <p> Public void onclick (dialoginterface dialog, <br/> int which) {<br/>}</P> <p> }). show (); <br/>}</P> <p >}; </P> <p> // create a listdialog, option dialog box <br/> string [] operas = new string [] {"open", "RENAME", "delete" };< br/> New alertdialog. builder (filemanageractivity. this) <br/>. settitle ("operator dialog "). setitems (operas, clicklistener) <br/>. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {</P> <p> Public void onclick (dialoginterface dialog, int which) {<br/>}</P> <p> }). show (); <br/>}</P> <p> // file opening method <br/> private void openfile (file F) {<br/> intent = new intent (); <br/> intent. addflags (intent. flag_activity_new_task); <br/> intent. setaction (Android. content. intent. action_view); </P> <p> // obtain the file media type <br/> string type = getmimetype (f); <br/> intent. setdataandtype (URI. fromfile (F), type); <br/> startactivity (intent ); <br/>}</P> <p> // obtain the MIME type <br/> private string getmimetype (file F) {<br/> string type = ""; <br/> string filename = f. getname (); <br/> string end = filename. substring (filename. indexof (". ") + 1 ). tolowercase (); <br/> // determine the file type <br/> If (end. equals ("m4a") | end. equals ("MP3") | end. equals ("mid") <br/> | end. equals ("xmf") | end. equals ("Ogg") | end. equals ("WAV") {<br/> type = "audio"; <br/>}else if (end. equals ("3GP") | end. equals ("MP4") {<br/> type = "video"; <br/>}else if (end. equals ("jpg") | end. equals ("GIF") | end. equals ("PNG") <br/> | end. equals ("Jpeg") | end. equals ("BMP") {<br/> type = "image"; <br/>}else {<br/> type = "*"; <br/>}< br/> // The Mime Type format is "file type/file extension" <br/> type + = "/*"; <br/> return type; <br/>}< br/>}
Enter the code in the XML on the home page:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: background = "@ drawable/White" <br/> <textview <br/> Android: Id = "@ + ID/XPath" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: padding = "5px" <br/> Android: textsize = "18sp" <br/> Android: textcolor = "@ drawable/Blue" <br/> <listview <br/> Android: id = "@ Android: ID/List" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> </P> <p> </linearlayout>
In addition, to display icons and file names in the column of the file list, you need to add a layout file and a custom adapter:
Code in the new layout file:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "horizontal" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <imageview <br/> Android: id = "@ + ID/imageicon" <br/> Android: layout_width = "30dip" <br/> Android: layout_height = "30dip"> <br/> </imageview> <br/> <textview <br/> Android: id = "@ + ID/filenametext" <br/> Android: layout_gravity = "center_vertical" <br/> Android: layout_width = "0dip" <br/> Android: layout_weight = "1.0" <br/> Android: layout_height = "wrap_content" <br/> Android: textcolor = "@ drawable/Black" <br/> </linearlayout>
Code in the Custom adapter:
Package COM. hoperun. adapter; </P> <p> Import Java. io. file; <br/> Import Java. util. list; </P> <p> Import COM. hoperun. activity. r; </P> <p> Import android. content. context; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmapfactory; <br/> Import android. view. layoutinflater; <br/> Import android. view. view; <br/> Import android. view. viewgroup; <br/> Import android. widget. baseadapter; <br/> Import android. widget. imageview; <br/> Import android. widget. textview; </P> <p> public class filelistadapter extends baseadapter {</P> <p> private list <string> items; <br/> private list <string> paths; <br/> private layoutinflater Inflater; <br/> private bitmap rooticon; <br/> private bitmap previcon; <br/> private bitmap docicon; <br/> private bitmap foldericon; </P> <p> Public filelistadapter () {}< br/> Public filelistadapter (context, list <string> items, <br/> List <string> paths) {<br/> // obtain a layout Loader from context <br/> Inflater = layoutinflater. from (context); <br/> This. items = items; <br/> This. paths = paths; <br/> // initialization Association icon <br/> rooticon = bitmapfactory. decoderesource (context. getresources (), <br/> r. drawable. back_root); <br/> previcon = bitmapfactory. decoderesource (context. getresources (), <br/> r. drawable. back_prev); <br/> docicon = bitmapfactory. decoderesource (context. getresources (), <br/> r.drawable.doc); <br/> foldericon = bitmapfactory. decoderesource (context. getresources (), <br/> r. drawable. folder); </P> <p >}</P> <p> // number of records returned for data <br/> Public int getcount () {<br/> return items. size (); <br/>}</P> <p> // return data in the list <br/> Public object getitem (INT position) {<br/> return items. get (position); <br/>}</P> <p> // return the item id value <br/> Public long getitemid (INT position) {<br/> return position; <br/>}</P> <p> // obtain the item component container in listview <br/> Public View getview (INT position, view convertview, viewgroup parent) {<br/> viewholder holder = NULL; </P> <p> If (convertview = NULL) {<br/> convertview = Inflater. inflate (R. layout. list_row, null); <br/> // create a sub-component container <br/> holder = new viewholder (); <br/> holder. TEXT = (textview) convertview. findviewbyid (R. id. filenametext); <br/> holder. imageicon = (imageview) convertview. findviewbyid (R. id. imageicon); </P> <p> convertview. settag (holder); <br/>}else {<br/> holder = (viewholder) convertview. gettag (); <br/>}</P> <p> // display different icons and file names based on different file types <br/> If (items. get (position ). equals ("back to/") {<br/> holder. imageicon. setimagebitmap (rooticon); <br/>} else if (items. get (position ). equals ("back previous") {<br/> holder. imageicon. setimagebitmap (previcon); <br/>}else {<br/> file F = new file (paths. get (position); <br/> If (F. isdirectory () {<br/> holder. imageicon. setimagebitmap (foldericon); <br/>} else {<br/> holder. imageicon. setimagebitmap (docicon); <br/>}< br/> holder. text. settext (items. get (position); <br/> return convertview; <br/>}</P> <p> // internal class, container class <br/> private class viewholder {<br/> textview text; <br/> imageview imageicon; <br/>}</P> <p >}< br/>
You also need to create the layout file content related to alertdialog In the rename file dialog box:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: Orientation = "vertical" <br/> <textview <br/> Android: id = "@ + ID/mtext" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_marginleft = "10dip" <br/> Android: layout_marginright = "10dip" <br/> Android: text = "@ string/str_text" <br/> Android: gravity = "Left" <br/> <edittext <br/> Android: Id = "@ + ID/nameedit" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_width = "200px" <br/> Android: layout_marginleft = "10dip" <br/> Android: layout_marginright = "10dip" <br/> Android: AutoText = "false" <br/> Android: gravity = "fill_horizontal" <br/> </linearlayout>
In this way, you can run it at the end to view the running effect...