一:程序所显示的Activity
public class FileShow extends Activity implements android.view.View.OnClickListener{
private void init() {
btnBack = (Button) findViewById(R.id.back); btnHome = (Button) findViewById(R.id.home); btnBack.setOnClickListener(this); btnHome.setOnClickListener(this);
tvTitle = (TextView) findViewById(R.id.tvTitle);
lvFiles = (ListView) findViewById(android.R.id.list);
//browseTo方法访问mnt下的sdcard目录,此方法返回目录中的所有子目录和private ListView lvFiles;
private ArrayList private Button btnBack, btnHome; 文件填充到items集合 browseTo(new File(\"/mnt/sdcard\")); adapter = new FileListAdapter(this, items); lvFiles.setAdapter(adapter); } // 浏览根目录 private void browseRoot() { browseTo(new File(\"/mnt/sdcard\")); } // 浏览上级目录 private void browseUpLevel() { if (current_dir.getParent() != null) { browseTo(current_dir.getParentFile()); } } // 浏览指定目录 private void browseTo(File dir) { // 如果dir对象是一个目录 if (dir.isDirectory()) { // 改变标题栏的标题 tvTitle.setText(dir.getAbsolutePath()); // 更改当前目录为指定目录 this.current_dir = dir; // 查找dir目录中的所有子目录和文件 填充到items集合 fill(current_dir.listFiles()); } } private void fill(File[] files) { // 如果items未初始化则初始化 if (items == null) { items = new ArrayList // 清空items中所存储的原目录信息 items.clear(); Resources res = getResources(); if (files != null) { // 遍历当前目录中的所有文件和子目录 for (File file : files) { // 获取文件名 String fileName = file.getName(); // 获取文件或目录的图标 Drawable icon = null; // 如果是目录,则图标为folder if (file.isDirectory()) { icon = res.getDrawable(R.drawable.folder); } else { // 如果测试为音频文件,设置图标 if (checkEnd(fileName, res.getStringArray(R.array.audioFile))) { icon = res.getDrawable(R.drawable.audio); } // 如果测试为图片文件,设置图标 else if (checkEnd(fileName, res.getStringArray(R.array.imageFile))) { icon = res.getDrawable(R.drawable.image); } else if (checkEnd(fileName, res.getStringArray(R.array.packageFile))) { icon = res.getDrawable(R.drawable.packed); } else if (checkEnd(fileName, res.getStringArray(R.array.webFile))) { icon = res.getDrawable(R.drawable.webtext); } else { icon = res.getDrawable(R.drawable.text); } } // 创建fileitem对象,并添加到集合 FileItem item = new FileItem(fileName, icon); } items.add(item); } } // 如果adapter不为null 则通知更新界面 if (adapter != null) adapter.dataChanged(items); } // 检查指定文件名 是否以数组中指定的扩展名结尾 private boolean checkEnd(String fileName, String[] endType) { if (fileName != null && endType != null) { for (String end : endType) { if (fileName.endsWith(end)) return true; } } return false; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.back: browseUpLevel(); break; case R.id.home: browseRoot(); break; } } 二:Adapter: public class FileListAdapter extends BaseAdapter { ArrayList FileListAdapter(Context context,ArrayList public items){ this.inflater = LayoutInflater.from(context); this.items = items; } public void dataChanged(ArrayList @Override public int getCount() { } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override return items.size(); this.items = items; this.notifyDataSetChanged(); public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if(convertView==null){ convertView = inflater.inflate(R.layout.item, null); holder = new ViewHolder(); holder.ivFileIcon = = (ImageView)convertView.findViewById(R.id.ivFileIcon); holder.tvFileName (TextView)convertView.findViewById(R.id.tvFileName); convertView.setTag(holder); }else{ holder = (ViewHolder)convertView.getTag(); } FileItem item = items.get(position); holder.ivFileIcon.setImageDrawable(item.getFileIcon()); holder.tvFileName.setText(item.getFileName()); return convertView; } class ViewHolder{ ImageView ivFileIcon; TextView tvFileName; } } 三:item public class FileItem { public FileItem() { super(); } public FileItem(String fileName, Drawable fileIcon) { super(); this.fileName = fileName; this.fileIcon = fileIcon; } String fileName; Drawable fileIcon; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public Drawable getFileIcon() { return fileIcon; } public void setFileIcon(Drawable fileIcon) { this.fileIcon = fileIcon; } } 四:配置文件: 因篇幅问题不能全部显示,请点此查看更多更全内容