主界面就是显示两个button,分别进入File与Cache的目录:

程序的架构为:

来看下主程序的代码:

public class FileCacheActivity extends Activity implements OnClickListener { private Button myButton1; private Button myButton2; private File cacheDir; private File fileDir; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myButton1 = (Button) findViewById(R.id.myButton1); myButton2 = (Button) findViewById(R.id.myButton2); myButton1.setOnClickListener(this); myButton2.setOnClickListener(this); /** * 取得目前Cache目录 */ cacheDir = this.getCacheDir(); /** * 取得目前File目录 */ fileDir = this.getFilesDir(); } /* 调用EX05_16_1并将路径传入 */ private void showActivity(String path) { Intent intent = new Intent(); intent.setClass(FileCacheActivity.this, FileCacheListActivity.class); Bundle bundle = new Bundle(); bundle.putString("path", path); intent.putExtras(bundle); startActivity(intent); } @Override public void onClick(View v) { // TODO Auto-generated method stub int viewId = v.getId(); switch (viewId) { case R.id.myButton1: { String path = fileDir.getParent() + File.separator + fileDir.getName(); showActivity(path); break; } case R.id.myButton2: { String path = cacheDir.getParent() + File.separator + cacheDir.getName(); showActivity(path); break; } default: break; } } }

然后是显示listview的代码:

public class FileCacheListActivity extends Activity implements OnItemClickListener { private List<String> items = null; private String path; private ListView listview; /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mylist); listview = (ListView) findViewById(R.id.list); listview.setOnItemClickListener(this); /** * 取得bundle */ Bundle bunde = this.getIntent().getExtras(); /** * 取得上一个页面传入bundle里的数据 */ path = bunde.getString("path"); this.setTitle(path); /** * 去一个新的文件目录用来显示目录里的所有文件 */ File file = new File(path); fill(file.listFiles());// 列出该路径下的所有文件 } private void fill(File[] files) { items = new ArrayList<String>(); if (files == null) { return; } /* 取得文件名放入ArrayList */ for (File file : files) { items.add(file.getName()); } /* 将ArrayList放入ArrayAdapter */ ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); listview.setAdapter(fileList); } @Override public void onItemClick(AdapterView<?> l, View v, int position, long id) { File file = new File(path + File.separator + items.get(position)); if (file.isDirectory()) { fill(file.listFiles()); } } }

最后再来两个xml文件

main.xml

<?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" > <Button android:id="@+id/myButton1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/strList1" > </Button> <Button android:id="@+id/myButton2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/strList2" > </Button> </LinearLayout>

mylist.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ListView> </LinearLayout>

完了!

更多相关文章

  1. Android(安卓)自定义CheckBox样式
  2. Android(安卓)不使用布局文件,动态地生产布局显示
  3. Android(安卓)Studio技巧 之 自动命名APK文件
  4. Android下载文件提示文件不存在。。。 java.io.FileNotFoundExce
  5. Android中String.xml: The reference to entity "timestamp" mus
  6. Eclipse打开Android工程
  7. vitamio for android的使用
  8. Android播播放完SD卡指定文件夹音乐之后,自动播放下一首
  9. [Android]使用platform密钥来给apk文件签名的命令

随机推荐

  1. Android解析微博小尾巴
  2. android 关于gallery 的平滑代码滚动
  3. Android(安卓)O 新特性介绍:自适应图标(Ada
  4. android WebView结合javascript相互调用
  5. 如何获取Android设备唯一识别码
  6. android 实现圆形进度条
  7. Android(安卓)Studioto同步失败的解决笔
  8. android 计算器,纯手打,功能实现了,bug可能
  9. android Animation介绍
  10. android 进度对话框 圈