Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1293779
  • 博文数量: 175
  • 博客积分: 2743
  • 博客等级: 少校
  • 技术积分: 4024
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-30 01:41
文章分类

全部博文(175)

文章存档

2015年(1)

2013年(53)

2012年(71)

2011年(50)

分类: Java

2011-11-25 17:12:31

     


  1. 1. import java.io.File;
  2.    2. import java.util.*;
  3.    3.
  4.    4. import android.app.Activity;
  5.    5. import android.content.Context;
  6.    6. import android.os.*;
  7.    7. import android.view.*;
  8.    8. import android.widget.*;
  9.    9. import android.widget.AdapterView.OnItemClickListener;
  10.   10. import android.widget.ImageView.ScaleType;
  11.   11.
  12.   12. public class FileBrowser extends Activity {
  13.   13.
  14.   14. private ListView mainListView=null;
  15.   15. private List<Map<String,Object>> list=null;
  16.   16.
  17.   17. public void onCreate(Bundle savedInstanceState) {
  18.   18. super.onCreate(savedInstanceState);
  19.   19. this.setTitle("文件浏览器");
  20.   20. mainListView=new ListView(this);
  21.   21. setContentView(mainListView);
  22.   22.
  23.   23. File file=Environment.getRootDirectory();
  24.   24. String pathx=file.getAbsolutePath();
  25.   25. this.setTitle(pathx);
  26.   26. //android的总目录就是"/"
  27.   27. list_init("/");
  28.   28. }
  29.   29.
  30.   30. void list_init(String path){
  31.   31. File file=new File(path);
  32.   32. File[] fileList=file.listFiles();
  33.   33. list=new ArrayList<Map<String,Object>>();
  34.   34. Map<String,Object> item;
  35.   35. item=new HashMap<String,Object>();
  36.   36. if(path.equals("/")){
  37.   37. item.put("ico",R.drawable.home);
  38.   38. item.put("name","总目录列表");
  39.   39. item.put("path","/");
  40.   40. list.add(item);
  41.   41. }else{
  42.   42. item.put("ico",R.drawable.back);
  43.   43. item.put("name","返回上一级");
  44.   44. item.put("path",file.getParent());
  45.   45. list.add(item);
  46.   46. }
  47.   47. for(int i=0;i<fileList.length;i++){
  48.   48. item=new HashMap<String,Object>();
  49.   49. if(fileList[i].isDirectory()){
  50.   50. if(fileList[i].list().length<1){
  51.   51. item.put("ico",R.drawable.file1);
  52.   52. }else{
  53.   53. item.put("ico",R.drawable.file2);
  54.   54. }
  55.   55. }else{
  56.   56. item.put("ico",R.drawable.content);
  57.   57. }
  58.   58. item.put("name",fileList[i].getName());
  59.   59. item.put("path",fileList[i].getAbsolutePath());
  60.   60. list.add(item);
  61.   61. }
  62.   62. MyAdapter ma=new MyAdapter(this,list);
  63.   63. //mainListView=new ListView(this);
  64.   64. mainListView.setAdapter(ma);
  65.   65. mainListView.setOnItemClickListener(new OnItemClickListener(){
  66.   66. public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
  67.   67. if(arg2>0 && (Integer)(list.get(arg2).get("ico"))==R.drawable.content){
  68.   68. //非文件夹图标,点击无效
  69.   69. }else{
  70.   70. //打开下一级文件目录列表
  71.   71. list_init((String)(list.get(arg2).get("path")));
  72.   72. }
  73.   73. }
  74.   74. });
  75.   75. this.setTitle(path);
  76.   76. }
  77.   77.
  78.   78. public class MyAdapter extends BaseAdapter{
  79.   79.
  80.   80. Context context=null;
  81.   81. List<Map<String,Object>> list=null;
  82.   82.
  83.   83. MyAdapter(Context context,List<Map<String,Object>> list){
  84.   84. this.context=context;
  85.   85. this.list=list;
  86.   86. }
  87.   87. public int getCount() {return list.size();}
  88.   88. public Object getItem(int position) {return position;}
  89.   89. public long getItemId(int position) {return position;}
  90.   90.
  91.   91. public View getView(int position, View convertView, ViewGroup parent) {
  92.   92. LinearLayout returnView=new LinearLayout(context);
  93.   93. returnView.setLayoutParams(new ListView.LayoutParams(-1,-2));//注意:ListView.LayoutParams
  94.   94. //图标
  95.   95. ImageView iv=new ImageView(context);
  96.   96. LinearLayout.LayoutParams lp_iv=new LinearLayout.LayoutParams(-2,-2);
  97.   97. lp_iv.rightMargin=10;
  98.   98. iv.setLayoutParams(lp_iv);
  99.   99. iv.setScaleType(ScaleType.CENTER_INSIDE);
  100.  100. iv.setImageResource((Integer)((list.get(position)).get("ico")));
  101.  101. returnView.addView(iv);
  102.  102. //文件名
  103.  103. TextView name=new TextView(context);
  104.  104. LinearLayout.LayoutParams lp_tv=new LinearLayout.LayoutParams(-2,-2);
  105.  105. name.setLayoutParams(lp_tv);
  106.  106. name.setTextSize(name.getTextSize()+10);
  107.  107. name.setText((String)(list.get(position).get("name")));
  108.  108. returnView.addView(name);
  109.  109. //
  110.  110. return returnView;
  111.  111. }
  112.  112.
  113.  113. }
  114.  114. }
阅读(3489) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~