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

        这要对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

更多相关文章

  1. Activity的创建
  2. Eclipse中跟踪调试Android(安卓)Framework源代码
  3. Appium+Python实现iOS自动化测试~环境搭建
  4. Android基于UDP的局域网聊天通信(有完整Demo)
  5. 三种定时器的使用
  6. Android(安卓)Support Percent百分比布局
  7. 获取Android(安卓)ics源码
  8. android中google“定位查询”
  9. FlexboxLayout实现流式布局

随机推荐

  1. [android] EditText的setError文字不显示
  2. android android屏幕禁止休眠和锁屏的方
  3. Android启动过程
  4. 第九章 Android(安卓)系统信息与安全机制
  5. Android自带音频均衡器MusicFx分析
  6. android ADB 详解
  7. Android关闭JIT的方法
  8. Android官方架构组件ViewModel:从前世今
  9. android同一个程序中使用多个地图出现混
  10. Android(安卓)实现TextView中文字链接的