打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android

主程序的实现代码如下:

public class EX04_21 extends ListActivity
{
  /* 对象声明
    items:存放显示的名称
    paths:存放文件路径
    rootPath:起始目录
  */
  private List<String> items=null;
  private List<String> paths=null;
  private String rootPath="/";
  private TextView mPath;
 
  /** Called when the activity is first created. */
  @Override
  protected void onCreate(Bundle icicle)
  {
    super.onCreate(icicle);
   
    /* 加载main.xml Layout */
    setContentView(R.layout.main);
    mPath=(TextView)findViewById(R.id.mPath);
   
    getFileDir(rootPath);
  }
 
  /* 取得文件架构的method */
  private void getFileDir(String filePath)
  {
    /* 设定目前所在路径 */
    mPath.setText(filePath);
   
    items=new ArrayList<String>();
    paths=new ArrayList<String>(); 
    File f=new File(filePath); 
    File[] files=f.listFiles();

    if(!filePath.equals(rootPath))
    {
      /* 第一笔设定为[回到根目录] */
      items.add("Back to "+rootPath);
      paths.add(rootPath);
      /* 第二笔设定为[回上层] */
      items.add("Back to ../");
      paths.add(f.getParent());
    }
    /* 将所有文件加入ArrayList中 */
    for(int i=0;i<files.length;i++)
    {
      File file=files[i];
      items.add(file.getName());
      paths.add(file.getPath());
    }
   
    /* 声明一ArrayAdapter,使用file_row这个Layout,
                  并将Adapter设定给此ListActivity */
    ArrayAdapter<String> fileList =
      new ArrayAdapter<String>(this,R.layout.file_row, items);
    setListAdapter(fileList);
  }
 
  /* 设定ListItem被按下时要做的动作 */
  @Override
  protected void onListItemClick(ListView l,View v,int position,long id)
  {
    File file = new File(paths.get(position));
    if(file.canRead())
    {
      if (file.isDirectory())
      {
        /* 如果是文件夹就再进去读取 */
        getFileDir(paths.get(position));
      }
      else
      {
        /* 如果是文件,则弹出AlertDialog */
        new AlertDialog.Builder(this)
            .setTitle("Message")
            .setMessage("["+file.getName()+"] is File!")
            .setPositiveButton("OK",
              new DialogInterface.OnClickListener()
              {
                public void onClick(DialogInterface dialog,int which)
                {
                }
              }).show();       
      }
    }
    else
    {
      /* 弹出AlertDialog显示权限不足 */
      new AlertDialog.Builder(this)
          .setTitle("Message")
          .setMessage("权限不足!")
          .setPositiveButton("OK",
            new DialogInterface.OnClickListener()
            {
              public void onClick(DialogInterface dialog,int which)
              {
              }
            }).show();   
    }
  }
}

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
androidwebviewjsalert对话框不能弹出解决办法
Android 动态权限申请 BaseActivity 封装 拨打电话
AlertDialog的六种创建方式
android 弹出选择框,简单通用弹出选择框
C#获取本地磁盘目录源代码
AlertDialog弹出自定义对话框
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服