查找程序根目录下所有文件(Java IO与ListActivity的结合)

来源:互联网 发布:淘宝详情可以改动吗 编辑:程序博客网 时间:2024/06/05 06:17

查找程序根目录下所有文件(Java IO与ListActivity的结合)

新建一个继承Activity类的FileBroswerActivity,并设置布局文件为:filebroswer.xml。

首先在布局文件添加一个ListView,和一个TextView组件。这里的ListView组件的ID必须是系统的Id:list

<?xmlversion="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

 

    <TextView

        android:id="@+id/filebroswer_tv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/show_information"

        android:textSize="20sp"/>

 

 

    <ListView

        android:id="@android:id/list"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"/>

 

</LinearLayout>

而后在Activity代码中实现。

package lyx.feng.second;

......

public class FileBroswerActivity extends ListActivity {

    private TextViewtv = null;

    private static final String ROOT ="/";

    //保存文件名

    private ArrayList<String>item = null;

    //保存路径

    private ArrayList<String>path = null;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       super.setContentView(R.layout.filebroswer);

       this.tv = (TextView)super.findViewById(R.id.filebroswer_tv);

       //默认从根目录开始

       showListFile(ROOT);

    }

 

    private void showListFile(String string) {

       //初始化一些参数

       this.tv.setText("当前的目录是:" + string);

       this.item =new ArrayList<String>();

       this.path =new ArrayList<String>();

       File file = new File(string);

       File listFiles[] = file.listFiles();

       if (!string.equals(ROOT)) {

           //不是根目录就添加2个返回Item

           this.item.add("Back To: " +ROOT);

           this.path.add(ROOT);

 

           this.item.add("Back To: " + string);

           this.path.add(file.getParent());

       }

       for (int i = 0; i < listFiles.length; i++) {

           //添加每一个Item

           item.add(listFiles[i].getName());

           path.add(listFiles[i].getPath());

       }

       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,

              android.R.layout.simple_list_item_1,this.item);

       //设置Adapter

       super.setListAdapter(adapter);

    }

 

    @Override

    protected void onListItemClick(ListView l, View v, int position,long id) {

       super.onListItemClick(l, v, position, id);

       File file = new File(path.get(position));

       if (file.canRead()) {

           //可读的文件操作

           if (file.isDirectory()) {

              //继续调用查找文件

              showListFile(path.get(position));

           } else {

              Toast.makeText(this,"文件", Toast.LENGTH_LONG).show();

           }

       } else {

           Toast.makeText(this,"没有权限", Toast.LENGTH_LONG).show();

       }

    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0 0
原创粉丝点击