zoukankan      html  css  js  c++  java
  • Android简单的文件浏览器,ListActivity的简单用法

    
    
    2014-07-29 13:39:09
    MainActivity.java
    package com.example.sample_4_21;
    
    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.KeyEvent;
    import android.view.View;
    import android.view.Window;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends ListActivity {
        List<String> filelist = null;
        String rootPath = "/";
        String currentpath;
        String parentPath;
        ArrayAdapter<String> adapter;
        ArrayAdapter<String> maAdapter;
        TextView tv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);
            tv = (TextView) findViewById(R.id.mPath);
            init(rootPath);
        }
    
        void init(String path) {
            adapter = new ArrayAdapter<String>(this, R.layout.file_row,
                    getFileList(path));
            setListAdapter(adapter);
            tv.setText(path);
            currentpath = path;
        }
    
        List<String> getFileList(String path) {
            File f = new File(path);
            File[] allfile = f.listFiles();
            filelist = new ArrayList<String>();
            parentPath = f.getParent();
            if (parentPath==null) {//如果为根目录,则f.getparent()为null
                parentPath = "/";
            }
            filelist.add("back to..." +parentPath);
            for (File file : allfile) {
                filelist.add(file.getPath());
            }
            return filelist;
        }
    
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            // TODO Auto-generated method stub
            super.onListItemClick(l, v, position, id);
            if (position == 0) {
                if (currentpath == rootPath) {
                    Toast.makeText(this, "为根目录", Toast.LENGTH_SHORT).show();
                } else {
                    init(parentPath);
                }
            } else {
                if (new File(filelist.get(position)).isFile()) {
                    Toast.makeText(this, "文件", Toast.LENGTH_SHORT).show();
                } else if (!new File(filelist.get(position)).canRead()) {
                    Toast.makeText(this, "不可读", Toast.LENGTH_SHORT).show();
                } else {
                    init(filelist.get(position));
                }
            }
    
        }
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_UP){
                if(currentpath==rootPath){
                    new AlertDialog.Builder(this)
                    .setTitle("提示")
                    .setMessage("确定退出吗")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            MainActivity.this.finish();
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                        }
                    })
                    .show();
                    return true;
                }else{
                    init(parentPath);
                    return false;
                }    
            }else{
                return false;
            }
        }
    }
    
    
    
     
       activity_main.xml  
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${packageName}.${activityClass}" > <TextView android:id="@+id/mPath" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:paddingLeft="10dp" android:textColor="#00ff33" /> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_below="@+id/mPath" android:paddingLeft="10dp" android:layout_height="wrap_content" /> </RelativeLayout>
    file_row.xml list——item项
    <?xml version="1.0" encoding="utf-8"?> <TextView android:id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="20dp" android:textSize="15sp" android:textColor="#000000" />
  • 相关阅读:
    Linux日志不记录问题
    Centos下yum安装PHP
    centos yum update kernel
    oh-my-zsh主题
    centos 6.6 使用tomcat6部署solr5.3.1
    Nginx manifest 实现 HTML5 Application Cache
    -bash: /bin/rm: Argument list too long
    linux mysql-5.6.26 安装
    LVM 管理减少swap分区空间增加到根分区
    Linux 使用iftop命令查看服务器流量
  • 原文地址:https://www.cnblogs.com/mf0819/p/3875319.html
Copyright © 2011-2022 走看看