方便用户选择要导入 的文件或者 选择要导出和文件的保存路径。
一. 整体设计
整体设计中 ,我们完成 了界面 的初始化,并实现 了列表事的单击事件和长按事件、
二、实现 getFileDir()方法
用于显示目前 的文件路径 ,并显示 这个 路径下面的文件列表 。
(1) 通过传递进来 的文件路径 得到文件对象
(2)通过 File.listFiles()得到这个方法下面的子文件的列表 、
(3)将文件列表 下面的文件名和文件路径 取出来 分别 保存在两个List中,
(4)用新的List 构造 Adapter
(5)设置Adapter 进行界面 的显示
三。实现 returnFilePath()方法
用于得到文件的路径 ,并将之设定为Activity的 result,传递回发起端的 Activity
public class FileSearch extends ListActivity{
private List<String >items =null;
private List<String >paths =null;
private String rootPath ="/";
private ListView mList ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
mList =(ListView)findViewById(android.R.id.list);
mList.setOnItemLongClickListener(new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
File file = new File(paths.get(position));
if (!file.canRead()){
Toast.makeText(getBaseContext(), "权限不够", Toast.LENGTH_SHORT).show();
return true ;
}
returnFilePath(file);
return false;
}
});
getFileDir (rootPath);
Toast.makeText(getBaseContext(), "长按文件夹选择路径", Toast.LENGTH_SHORT).show();
}
protected void onListItemClick(ListView l ,View v ,int position ,long id ){
File file = new File(paths.get(position));
if (!file.canRead()){
Toast.makeText(getBaseContext(), "权限不够", Toast.LENGTH_SHORT).show();
return ;
}
if (file.isDirectory()){
getFileDir(paths.get(position));
}else {
returnFilePath(file);
}
}
private void getFileDir(String filePath) { //filePath ,传进来 的文件路径
// TODO Auto-generated method stub
this.setTitle(filePath); //显示当前 的路径
items= new ArrayList<String >();
paths = new ArrayList<String >();
File f= new File(filePath);
File [] files = f.listFiles(); //得到子文件列表
if (!filePath.equals(rootPath)){
items.add("backRoot");
paths.add(rootPath);
items.add("back");
paths.add(f.getParent()); //得到上级目录
}
for ( int i=0;i<files.length;i++){ //遍历 files文件
File file = files[i];
items.add(file.getName()); //得到文件名
paths.add(file.getPath());
}
setListAdapter (new MyAdapter(this ,items ,paths ));
}
protected void returnFilePath(File file) {
// TODO Auto-generated method stub
String filePath = file.getPath();
Intent i = getIntent();
Bundle bundle = new Bundle (); //Bunlde结构相当 于Map
bundle.putString("filePath", filePath);
i.putExtras(bundle);
setResult(RESULT_OK,i);
finish();
}
四,实现 MyAdapter类
文件名列表 与视图有效地绑定在一起,这个 时候 我们要有两个数据 源,文件名的list 和文件路径 的list .
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater ;
private Bitmap bm1;
private Bitmap bm2;
private Bitmap bm3;
private Bitmap bm4;
private List<String>items;
private List <String>paths;
public MyAdapter(Context context, List<String> item,
List<String> path) {
mInflater = LayoutInflater.from(context);
items = item;
paths = path;
bm1= BitmapFactory.decodeResource(context.getResources(),
R.drawable.back01);
bm2 = BitmapFactory.decodeResource(context.getResources(),
R.drawable.back02);
bm3= BitmapFactory.decodeResource(context.getResources(),
R.drawable.folder);
bm4= BitmapFactory.decodeResource(context.getResources(),
R.drawable.doc);
}
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder ;
if (convertView==null){
convertView = mInflater.inflate(R.layout.file_row, null);
holder = new ViewHolder();
holder.text =(TextView)convertView.findViewById(R.id.text);
holder.icon=(ImageView)convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
File f = new File(paths.get(position).toString());
if (items.get(position).toString().equals("backRoot")){
holder.text.setText("返回根目录");
holder.icon.setImageBitmap(bm1);
}else if (items.get(position).toString().equals("back")){
holder.text.setText("返回上一级");
holder.icon.setImageBitmap(bm2);
}else {
holder.text.setText(f.getName());
if (f.isDirectory()){
holder.icon.setImageBitmap(bm3);
}else {
holder.icon.setImageBitmap(bm4);
}
}
return convertView;
}
private TextView findViewById(int text) {
// TODO Auto-generated method stub
return null;
}
private class ViewHolder{
TextView text;
ImageView icon;
}
}