Andoid -- select the file directory

Source: Internet
Author: User


Layout file activity_main.xml of the main activity


<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">

<Button
Android: Id = "@ + ID/filedire_button1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "select file"/>

<Textview
Android: Id = "@ + ID/filedire_textview1"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: text = ""/>

</Linearlayout>


The image is shown as follows:



The layout file Adapter. xml required to create the adapter


<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: Orientation = "vertical">

<Listview
Android: Id = "@ + ID/adapter_listview1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
</Listview>

<Linearlayout
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: Orientation = "horizontal">

<Imageview
Android: Id = "@ + ID/adapter_image"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_weight = "0.48"
Android: src = "@ drawable/ic_launcher"/>

<Textview
Android: Id = "@ + ID/adapter_filename"
Android: layout_width = "242dp"
Android: layout_height = "match_parent"
Android: text = "textview"/>
</Linearlayout>

</Linearlayout>


Main activity program mainactivity. Java

Package com. example. filedirec;

Import java. Io. file;
Import java. util. arraylist;
Import java. util. hashmap;
Import java. util. List;
Import java. util. Map;

Import Android. OS. Bundle;
Import Android. App. activity;
Import Android. App. alertdialog;
Import Android. App. alertdialog. Builder;
Import Android. content. dialoginterface;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. simpleadapter;
Import Android. widget. textview;

Public class mainactivity extends activity {
Private button choosefile;
Private textview filedire;
Private string curpath = new string ("/");

@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. activity_main );

Choosefile = (button) findviewbyid (R. Id. filedire_button1 );
Filedire = (textview) findviewbyid (R. Id. filedire_textview1 );

Choosefile. setonclicklistener (New onclicklistener (){

@ Override
Public void onclick (view arg0 ){
// Todo auto-generated method stub
Opencurdir (curpath );
}
});
}

Private void opencurdir (string curpath ){
File F = new file (curpath );
File [] file = f. listfiles ();
Final list <Map <string, Object> listitem = new arraylist <Map <string, Object> ();

If (! Curpath. Equals ("/") {// Add this option to the list to be displayed if it is not the root directory
Map <string, Object> map1 = new hashmap <string, Object> ();
Map1.put ("name", "Back to the upper-level directory ");
Map1.put ("image", R. drawable. back02 );
Map1.put ("path", F. getparent ());
Map1.put ("isdire", true );
Listitem. Add (map1 );
}

If (file! = NULL) {// It must be determined. Otherwise, an error will be reported when the directory is empty.
For (INT I = 0; I <file. length; I ++ ){
Map <string, Object> map = new hashmap <string, Object> ();
Map. Put ("name", file [I]. getname ());
Map. Put ("image", (file [I]. isdirectory ())? R. drawable. Folder: r.drawable.doc );
Map. Put ("path", file [I]. getpath ());
Map. Put ("isdire", file [I]. isdirectory ());
Listitem. Add (MAP );
}
}

Simpleadapter adapter = new simpleadapter (mainactivity. This, listitem, R. layout. adapter,
New String [] {"name", "image"}, new int [] {R. Id. adapter_filename, R. Id. adapter_image });

Final alertdialog. Builder B = new Builder (mainactivity. This );
B. setadapter (adapter, new dialoginterface. onclicklistener (){

@ Override
Public void onclick (dialoginterface arg0, int arg1 ){
// Todo auto-generated method stub
If (Boolean) listitem. Get (arg1). Get ("isdire ")){
Opencurdir (string) listitem. Get (arg1). Get ("path "));
} Else {
Filedire. settext (string) listitem. Get (arg1). Get ("path "));
}
}
});
B. Show ();
}

}


The dialog box is displayed as follows:















Andoid -- select the file directory

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.