package com.Aina.Android;  
   
 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 Test_ListFile extends ListActivity {  
     /** Called when the activity is first created. */  
     private List items = null;//存放名称  
     private List paths = null;//存放路径  
     private String rootPath = "/";  
     private TextView tv;  
   
     @Override  
     public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
         tv = (TextView) this.findViewById(R.id.TextView);  
         this.getFileDir(rootPath);//获取rootPath目录下的文件.  
     }  
   
     public void getFileDir(String filePath) {  
         try{  
             this.tv.setText("当前路径:"+filePath);// 设置当前所在路径  
             items = new ArrayList();  
             paths = new ArrayList();  
             File f = new File(filePath);  
             File[] files = f.listFiles();// 列出所有文件  
             // 如果不是根目录,则列出返回根目录和上一目录选项  
             if (!filePath.equals(rootPath)) {  
                 items.add("返回根目录");  
                 paths.add(rootPath);  
                 items.add("返回上一层目录");  
                 paths.add(f.getParent());  
             }  
             // 将所有文件存入list中  
             if(files != null){  
                 int count = files.length;// 文件个数  
                 for (int i = 0; i < count; i++) {  
                     File file = files[i];  
                     items.add(file.getName());  
                     paths.add(file.getPath());  
                 }  
             }  
   
             ArrayAdapter adapter = new ArrayAdapter(this,  
                     android.R.layout.simple_list_item_1, items);  
             this.setListAdapter(adapter);  
         }catch(Exception ex){  
             ex.printStackTrace();  
         }  
   
     }  
   
     @Override  
     protected void onListItemClick(ListView l, View v, int position, long id) {  
         super.onListItemClick(l, v, position, id);  
         String path = paths.get(position);  
         File file = new File(path);  
         //如果是文件夹就继续分解  
         if(file.isDirectory()){  
             this.getFileDir(path);  
         }else{  
             new AlertDialog.Builder(this).setTitle("提示").setMessage(file.getName()+" 是一个文件!").setPositiveButton("OK", new DialogInterface.OnClickListener(){  
   
                 public void onClick(DialogInterface dialog, int which) {  
                                           
                 }  
                   
             }).show();  
         }  
     }  
       
 }  
 
 
 
 <?xml version="1.0" encoding="utf-8"?>  
       android:orientation="vertical" android:layout_width="fill_parent"  
     android:layout_height="fill_parent">  
              android:id="@+id/TextView"  
         android:background="#ffff00"  
         android:layout_height="wrap_content" android:text="@string/hello" />  
              android:layout_width="fill_parent"  
         android:layout_height="wrap_content">  
    
 
 

 

更多相关文章

  1. android 调用第三方软件打开pdf文件
  2. Android(安卓)通过HTTP POST 上传图片文件
  3. Android(安卓)studio 点击按钮 改变viewtext 文字的颜色
  4. android 一些路径的标准写法
  5. Android代码混淆官方实现方法
  6. Android(安卓)文件读写的例子
  7. android:xml解析
  8. Pycharm安装PyQt5的详细教程
  9. NPM 和webpack 的基础使用

随机推荐

  1. Android下Content Provider使用
  2. 一些摘抄
  3. Android OTA升级原理和流程分析(一)--updat
  4. Android_简单笔记一
  5. 【Android(安卓)学习笔记】save data(1)—
  6. 【Android】Broadcast控制音乐暂停继续等
  7. Android本地硬盘的API文档打开速度慢
  8. Android中的布局属性
  9. android兼容huawei手机刘海屏解决方案
  10. Android中如何设置RadioButton在文字的右