package com.shawn.zp.reader.activity; import java.io.File; import java.io.FileFilter; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.content.Context; import android.os.Bundle; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; public class AddBookActivity extends ListActivity{ private TextView currentFolder; //当前文件夹 private FileListAdapter fileList; //文件过滤器,查找文件夹或文本文件 private static final FileFilter BOOKS_FILTER = new FileFilter() { public boolean accept(File f) { return f.isDirectory() || f.getName().toLowerCase().matches("^.*?\\.(txt|text)$"); } }; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_addbook_directory); currentFolder = (TextView)findViewById(R.id.addbook_current_folder); fill(new File("/")); } public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && !fileList.isRoot) { fill(fileList.getItem(0)); return true; } return super.onKeyDown(keyCode, event); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { File file = fileList.getItem(position); if (file.isDirectory()) fill(file); } //跳转目录 private void fill(File folder) { boolean isRoot = folder.getParent() == null; currentFolder.setText(AddBookActivity.this.getResources().getString(R.string.addbook_current_folder) + folder.getAbsolutePath()); List files = new ArrayList (); if (!isRoot) files.add(folder.getParentFile()); File[] filterFiles = folder.listFiles(BOOKS_FILTER); if(null != filterFiles && filterFiles.length>0) { for (File file : filterFiles) { files.add(file); } } fileList = new FileListAdapter(this, R.layout.layout_addbook_file, files, isRoot); setListAdapter(fileList); } } //文件列表适配器 class FileListAdapter extends ArrayAdapter { private LayoutInflater mInflater; boolean isRoot; //根目录 public FileListAdapter(Context context, int Resource, List objects, boolean isRoot) { super(context,Resource,objects); this.isRoot = isRoot; this.mInflater = LayoutInflater.from(context); } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = mInflater.inflate(R.layout.layout_addbook_file, null); TextView title = (TextView)convertView.findViewById(R.id.addbook_title); TextView info = (TextView)convertView.findViewById(R.id.addbook_info); File file = getItem(position); if (position == 0 && !isRoot) title.setText(R.string.addbook_parent_folder); else { title.setText(file.getName()); if (file.isDirectory()) info.setText(R.string.addbook_directory); else { long fileSize = file.length(); if(fileSize >= 1024*1024) { float size = fileSize/(1024f*1024f); info.setText(new DecimalFormat("#.00").format(size) + "MB"); }else if(fileSize >= 1024) { float size = fileSize/1024f; info.setText(new DecimalFormat("#.00").format(size) + "KB"); }else { info.setText(fileSize + "B"); } } } return convertView; } }