模拟实现android 文件管理器功能(循环打开文件夹)


一、 这部分仅实现了文件夹循环打开,  完整模仿代码我在后面文章中会贴出来。

        这要对listview的使用有一定的了解, 还没入门的同学请看http://blog.csdn.net/xiaominmincs/article/details/7799986

看运行后的效果:


二、xml代码:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"   
  6.     >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/tv"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="sdcard目录" />  
  13.     <ListView   
  14.      android:id="@+id/lvfiles"  
  15.      android:layout_width="fill_parent"  
  16.     android:layout_height="wrap_content"  
  17.      >  
  18.       </ListView>  
  19.   
  20. </LinearLayout>  

三、 java代码:

[java]  view plain copy
  1. package xmm.Zt_07_storage;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7.   
  8. import android.app.Activity;  
  9. import android.content.Intent;  
  10. import android.os.Bundle;  
  11. import android.util.Log;  
  12. import android.view.LayoutInflater;  
  13. import android.view.View;  
  14. import android.view.ViewGroup;  
  15. import android.widget.AdapterView;  
  16. import android.widget.AdapterView.OnItemClickListener;  
  17. import android.widget.BaseAdapter;  
  18. import android.widget.ListView;  
  19. import android.widget.TextView;  
  20. import android.widget.Toast;  
  21.   
  22. public class Filestorage extends Activity {  
  23.     /** Called when the activity is first created. */  
  24.   
  25.     File f;  
  26.     private FileOutputStream out;  
  27.     private FileInputStream in;  
  28.     private BufferedReader reader;  
  29.     File[] arrf;  
  30.     private TextView tv1;  
  31.     ListView lvfile;  
  32.     Intent intents=new Intent();  
  33.     String path;  
  34.   
  35.     @Override  
  36.     public void onCreate(Bundle savedInstanceState) {  
  37.         super.onCreate(savedInstanceState);  
  38.         setContentView(R.layout.filestorage);  
  39.         TextView  tv=(TextView) findViewById(R.id.tv);  
  40.         lvfile = (ListView) findViewById(R.id.lvfiles);  
  41.         String compath = getIntent().getStringExtra("path");  
  42.         if (compath == "" || compath.equals("")) {  
  43.             compath= "/mnt/sdcard";  
  44.         }   
  45.          tv.setText(compath);  
  46.             f = new File(compath);  
  47.           
  48.         if (!(getSDcard() == 0)) {  
  49.             lvfile.setAdapter(new MyAdapter());  
  50.             lvfile.setOnItemClickListener(new OnItemClickListener() {  
  51.   
  52.                 public void onItemClick(AdapterView<?> arg0, View arg1,  
  53.                         int arg2, long arg3) {  
  54.                     // TODO Auto-generated method stub  
  55.                     path = arrf[arg2].getAbsolutePath();  
  56.                     tv1.setText(path);  
  57.                     intents.putExtra("path", path);  
  58.                   
  59.                     intents.setClass(Filestorage.this, Filestorage.class);  
  60.                     startActivity(intents);  
  61.                 }  
  62.             });  
  63.         }  
  64.   
  65.     }  
  66.   
  67.   
  68.   
  69.     public int getSDcard() {  
  70.         arrf = f.listFiles();  
  71.         if (arrf == null || arrf.equals(null)) {  
  72.             Toast.makeText(this"sd卡里面没有数据", Toast.LENGTH_LONG);  
  73.             return 0;  
  74.         } else {  
  75.             return 1;  
  76.         }  
  77.     }  
  78.   
  79.     public class MyAdapter extends BaseAdapter {  
  80.   
  81.         public int getCount() {  
  82.             // TODO Auto-generated method stub  
  83.             Log.e("---getCount", arrf.length + "");  
  84.             return arrf.length;  
  85.         }  
  86.   
  87.         public Object getItem(int arg0) {  
  88.             // TODO Auto-generated method stub  
  89.             return arrf[arg0];  
  90.         }  
  91.   
  92.         public long getItemId(int position) {  
  93.             // TODO Auto-generated method stub  
  94.             return position;  
  95.         }  
  96.   
  97.         public View getView(int position, View convertView, ViewGroup parent) {  
  98.             LayoutInflater inflater = getLayoutInflater();  
  99.             View row = inflater.inflate(R.layout.filerowlayout, null);  
  100.             tv1 = (TextView) row.findViewById(R.id.tvfile);  
  101.             tv1.setText(arrf[position].getName());  
  102.             return row;  
  103.         }  
  104.   
  105.     }  
  106.   
  107. }  

最后: 希望大家一起讨论! 谢谢!     

完整代码:http://115.com/file/dp932rc9#zt_07_storage.rar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值