<?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>


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;@Overridepublic 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();}}@Overrideprotected 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();}}}

更多相关文章

  1. Android(安卓)Button Maker(在线生成android shape xml文件的工具
  2. qt for android
  3. Android编译过程详解
  4. android tween动画效果
  5. Android(安卓)Studio Toast/Notification中文乱码解决办法
  6. Android之MotionLayout(一),MotionLayout的基本使用
  7. Android(二)HelloWorld,Android(上)
  8. Android应用开发相关下载资源(2014/12/14更新)
  9. Android(安卓)adb shell命令大全

随机推荐

  1. Android ViewGroup 触摸屏事件派发机制和
  2. Android 应用程序退出的四种方法
  3. Android动态分析工具Inspeckage
  4. Android Opencv 410 集成 以及 jni环境配
  5. iOS Airplay--Airtunes音乐播放在Android
  6. android画图---shape的使用
  7. android遍历sd卡中的所有文件
  8. 安装android驱动解决device not found
  9. Android 之 手机全屏显示
  10. Android 实现多个Audio文件的顺序播放