Android Introduction File system operation (a) Simple file browser

Source: Internet
Author: User

Android Introduction File system operation (a) Simple file browser (GO)

1.ImportJava.io.File; 2.ImportJava.util.*; 3. 4.Importandroid.app.Activity; 5.ImportAndroid.content.Context; 6.Importandroid.os.*; 7.Importandroid.view.*; 8.Importandroid.widget.*; 9.ImportAndroid.widget.AdapterView.OnItemClickListener; 10.ImportAndroid.widget.ImageView.ScaleType; 11. 12. Public classFileBrowserextendsActivity {13. 14.PrivateListView mainlistview=NULL; 15.PrivateList<map<string,object>> list=NULL; 16. 17. Public voidonCreate (Bundle savedinstancestate) {18.Super. OnCreate (savedinstancestate); 19. This. settitle ("File Browser"); mainlistview=.NewListView ( This); 21st. Setcontentview (Mainlistview); 22. . File file=environment.getrootdirectory (); . String pathx=File.getabsolutepath (); 25. This. Settitle (PATHX); 26.//The total Android directory is "/"List_init ("/"); 28. } 29. 30.voidlist_init (String path) {. File file=NewFile (path); File[] Filelist=File.listfiles (); List=.NewArraylist<map<string,object>>(); Map<string,object>.item; Item=.NewHashmap<string,object>(); 36.if(Path.equals ("/")){   Panax Notoginseng item.put ("ico"), R.drawable.home); Item.put ("name", "Total directory List"); Item.put ("Path", "/"); 40. List.add (item); 41.}Else{   Item.put ("ico"), R.drawable.back); Item.put ("name", "Return to previous level"); Item.put ("path"), File.getparent ()); 45. List.add (item); 46. } 47. for(inti=0;i<filelist.length;i++){   Item=.NewHashmap<string,object>(); 49.if(Filelist[i].isdirectory ()) {50.if(Filelist[i].list (). length<1){   Wuyi item.put ("ico"), R.drawable.file1); 52.}Else{   Item.put ("ico"), R.drawable.file2); 54. } 55.}Else{   Item.put ("ico"), r.drawable.content); 57. } Item.put ("name", Filelist[i].getname ()); Item.put ("path"), Filelist[i].getabsolutepath ()); 60. List.add (item); 61. } Myadapter ma=NewMyadapter ( This, list); 63.//mainlistview=new ListView (this);64. Mainlistview.setadapter (MA); Mainlistview.setonitemclicklistener (NewOnitemclicklistener () {66. Public voidOnitemclick (adapterview<?> arg0, View arg1,intArg2,LongArg3) {   67.if(Arg2>0 && (Integer) (List.get (arg2). Get ("ico")) = =r.drawable.content) {   68.//Non-folder icon, click Invalid69.}Else{   70.//Open the next Level file directory listList_init (String) (List.get (arg2). Get ("path"))); 72. } 73. } 74. }); 75. This. Settitle (path); 76. } 77. 78. Public classMyadapterextendsbaseadapter{79. Context context=NULL; Bayi. list<map<string,object>> list=NULL; 82. Myadapter (Context context,list<map<string,object>>list) {   84. This. context=context; 85. This. list=list; 86. } 87. Public intGetCount () {returnlist.size ();} 88. PublicObject GetItem (intPosition) {returnposition;} 89. Public LongGetitemid (intPosition) {returnposition;} 90. 91. PublicView GetView (intposition, View Convertview, ViewGroup parent) {   LinearLayout returnview=.Newlinearlayout (context); Returnview.setlayoutparams (NewListview.layoutparams ( -1,-2));//Note: Listview.layoutparams94.//iconsImageView iv=NewImageView (context); Linearlayout.layoutparams lp_iv=NewLinearlayout.layoutparams ( -2,-2); lp_iv.rightmargin=10.; 98. Iv.setlayoutparams (LP_IV); 99. Iv.setscaletype (Scaletype.center_inside); Iv.setimageresource ((Integer) ((List.get (position)). Get ("ico")))); 101. Returnview.addview (iv); 102.//file name103. TextView Name=NewTextView (context); 104. Linearlayout.layoutparams lp_tv=NewLinearlayout.layoutparams ( -2,-2); 105. Name.setlayoutparams (LP_TV); 106. Name.settextsize (Name.gettextsize () +10); 107. Name.settext (String) (List.get (position). Get ("name"))); 108. Returnview.addview (name); 109.//110.returnReturnview; 111. } 112. 113. } 114.}

Android Introduction File system operation (a) Simple file browser (GO)

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.