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<String> items = null;//存放名称
private List<String> 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<String>();
paths = new ArrayList<String>();
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<String> adapter = new ArrayAdapter<String>(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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:id="@+id/TextView"
android:background="#ffff00"
android:layout_height="wrap_content" android:text="@string/hello" />
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>

更多相关文章

  1. android之PopUpWindow显示Listview(文件列表)
  2. Android 不能生成R文件,gen目录为空
  3. Android 播放音乐文件与视频文件
  4. Android中下载文件的使用
  5. Android文件下载进度条的实现
  6. android直接读取数据库文件
  7. android 配置文件
  8. Android读取assets目录下所有文件

随机推荐

  1. Android动画之帧动画
  2. android hwcomposer 在视频播放中的应用
  3. Android之粘性广播理解
  4. Mac os下android源码下载及安装
  5. Android(安卓)Retrofit 源码系列(五)~ 设计
  6. Android判断当前网络是否可用--示例代码
  7. Android(安卓)homescreen 处理
  8. Android(安卓)SDK安装,无法连接google
  9. Android(安卓)UI(一)Layout 背景局部Shape
  10. Android必背内容(待续)