Android mini project-File Browser

Source: Internet
Author: User

The layout file will not be pasted. It is very simple to use two XML files. I try to clarify the comments of the Code.

Package COM. example. demofilebrower; import Java. io. file; import Java. io. ioexception; import Java. util. arraylist; import Java. util. hashmap; import Java. util. list; import Java. util. map; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. view; import android. view. view. onclicklistener; import android. widget. adapterview; import android. widget. adapterview. onitemclickl Istener; import android. widget. button; import android. widget. listview; import android. widget. simpleadapter; import android. widget. textview; import android. widget. toast; public class mainactivity extends activity {private textview showdir; private button; private listview; file currentparent; file [] currentfiles; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedins Tancestate); setcontentview (R. layout. activity_main); this. showdir = (textview) findviewbyid (R. id. textview1); this. button = (button) findviewbyid (R. id. button1); this. listview = (listview) findviewbyid (R. id. listview1); file root = new file ("/mnt/sdcard/"); If (root. exists () {currentparent = root; // The current parent path currentfiles = root. listfiles (); // all files in the current path inflatelistview (currentfiles);} listview. setonitemclicklis Tener (New onitemclicklistener () {public void onitemclick (adapterview <?> Parent, view, int position, long ID) {If (currentfiles [position]. isfile () {return;} file [] TMP = currentfiles [position]. listfiles (); If (TMP = NULL | TMP. length = 0) {toast. maketext (getapplicationcontext (), "No files in the current path or cannot be accessed", toast. length_long ). show () ;}else {currentparent = currentfiles [position]; currentfiles = TMP; inflatelistview (currentfiles) ;}}); // return the button in the upper-level directory. setonclicklistener (New on Clicklistener () {public void onclick (view v) {try {If (! Currentparent. getCanonicalPath (). equals ("/mnt/sdcard") {currentparent = currentparent. getparentfile (); currentfiles = currentparent. listfiles (); inflatelistview (currentfiles) ;}} catch (exception e) {}});} private void inflatelistview (file [] files) {// todo auto-generated method stublist <Map <string, Object> listitems = new arraylist <Map <string, Object> (); For (INT I = 0; I <files. length; I ++) {Map <String, Object> listitem = new hashmap <string, Object> (); If (files [I]. isdirectory () {// if it is a path, set the path icon listitem. put ("icon", R. drawable. ic_launcher);} else {// if it is a file, set the file icon. Listitem. put ("icon", R. drawable. ic_action_search);} listitem. put ("FILENAME", files [I]. getname (); listitems. add (listitem);} simpleadapter = new simpleadapter (this, listitems, R. layout. line, new string [] {"icon", "FILENAME"}, new int [] {R. id. imageview1, R. id. textview1}); listview. setadapter (simpleadapter); try {showdir. settext ("current path:" + currentparent. getCanonicalPath ();} catch (ioexception e) {e. printstacktrace () ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. activity_main, menu); Return true ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.