Android练习之ListActivity+JavaI/O=资源浏览器

code

 

View Code
  1 package com.example.flexd12;
  2 
  3 import java.io.File;
  4 import java.util.ArrayList;
  5 import java.util.List;
  6 
  7 import android.os.Bundle;
  8 import android.app.Activity;
  9 import android.app.AlertDialog;
 10 import android.app.ListActivity;
 11 import android.content.DialogInterface;
 12 import android.util.Log;
 13 import android.view.Menu;
 14 import android.view.View;
 15 import android.widget.ArrayAdapter;
 16 import android.widget.ListView;
 17 import android.widget.TextView;
 18 
 19 public class MainActivity extends ListActivity {
 20 
 21     private List items = null;//显示的名称们
 22     private List paths = null;//显示的路径们
 23     
 24     private String rootPath = "/";//根目录
 25     private TextView curPathTV;//当前目录
 26     
 27     @Override
 28     protected void onCreate(Bundle savedInstanceState) {
 29         super.onCreate(savedInstanceState);
 30         setContentView(R.layout.activity_main);
 31         
 32         curPathTV = (TextView) findViewById(R.id.tv1);
 33         //取得目录
 34         getFilePath(rootPath);
 35     }
 36     
 37     /**
 38      * 传入路径串,获取目录列表
 39      */
 40     private void getFilePath(String filePath){
 41         items = new ArrayList();
 42         paths = new ArrayList();
 43         
 44         File curFile = new File(filePath);//当前目录
 45         File[] curFiles = curFile.listFiles();//当前目录下文件
 46         
 47         //如果不是根目录,总是有回到根目录和回到上层的
 48         if(!filePath.equals(rootPath)){
 49             items.add(rootPath);
 50             paths.add(rootPath);
 51             
 52             items.add("../");
 53             paths.add(curFile.getParent());
 54         }
 55         
 56         //取得当前目录下的所有文件名、文件路径
 57         for(int i=0;i){
 58             File tempFile = curFiles[i];
 59             items.add(tempFile.getName()+ (tempFile.isDirectory()?" -DIR ":" -FILE "));
 60             paths.add(tempFile.getPath());
 61         }
 62         
 63         //ArrayAdapter的layout、items,并传入ListActivity
 64         ArrayAdapter fileList = new ArrayAdapter(this,R.layout.row_layout, items);
 65         setListAdapter(fileList);
 66     }
 67 
 68 
 69     /**
 70      * ListActivity列表项点击事件
 71      */
 72     @Override
 73     protected void onListItemClick(ListView l, View v, int position, long id) {
 74         
 75         //从paths取得路径,当前点击的路径
 76         File filePosition = new File(paths.get(position));
 77         curPathTV.setText(paths.get(position));
 78         
 79         if(filePosition.isDirectory()){
 80             //如果是目录,则再一次取列表
 81             Log.i("onListItemClick", paths.get(position));
 82             getFilePath(paths.get(position));
 83         }else{
 84             //如果是文件,弹出提示框
 85             new AlertDialog.Builder(this)
 86                     .setIcon(R.drawable.ic_launcher)
 87                     .setTitle("FileName:" + filePosition.getName())
 88                     .setPositiveButton("OK",
 89                             new DialogInterface.OnClickListener() {
 90 
 91                                 @Override
 92                                 public void onClick(DialogInterface dialog,
 93                                         int which) {
 94                                     // TODO Auto-generated method stub
 95                                 }
 96                             }).show();
 97         }
 98         
 99         // TODO Auto-generated method stub
100         super.onListItemClick(l, v, position, id);
101     }
102     
103     @Override
104     public boolean onCreateOptionsMenu(Menu menu) {
105         // Inflate the menu; this adds items to the action bar if it is present.
106         getMenuInflater().inflate(R.menu.activity_main, menu);
107         return true;
108     }
109 
110     
111 }

 

转载于:https://www.cnblogs.com/imzzh/archive/2013/01/08/2851023.html

你可能感兴趣的:(移动开发,java)