一、 这部分仅实现了文件夹循环打开, 完整模仿代码我在后面文章中会贴出来。
这要对listview的使用有一定的了解, 还没入门的同学请看http://blog.csdn.net/xiaominmincs/article/details/7799986
看运行后的效果:
二、xml代码:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- >
- <TextView
- android:id="@+id/tv"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="sdcard目录" />
- <ListView
- android:id="@+id/lvfiles"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- </ListView>
- </LinearLayout>
三、 java代码:
- package xmm.Zt_07_storage;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.BaseAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class Filestorage extends Activity {
- /** Called when the activity is first created. */
- File f;
- private FileOutputStream out;
- private FileInputStream in;
- private BufferedReader reader;
- File[] arrf;
- private TextView tv1;
- ListView lvfile;
- Intent intents=new Intent();
- String path;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.filestorage);
- TextView tv=(TextView) findViewById(R.id.tv);
- lvfile = (ListView) findViewById(R.id.lvfiles);
- String compath = getIntent().getStringExtra("path");
- if (compath == "" || compath.equals("")) {
- compath= "/mnt/sdcard";
- }
- tv.setText(compath);
- f = new File(compath);
- if (!(getSDcard() == 0)) {
- lvfile.setAdapter(new MyAdapter());
- lvfile.setOnItemClickListener(new OnItemClickListener() {
- public void onItemClick(AdapterView<?> arg0, View arg1,
- int arg2, long arg3) {
- // TODO Auto-generated method stub
- path = arrf[arg2].getAbsolutePath();
- tv1.setText(path);
- intents.putExtra("path", path);
- intents.setClass(Filestorage.this, Filestorage.class);
- startActivity(intents);
- }
- });
- }
- }
- public int getSDcard() {
- arrf = f.listFiles();
- if (arrf == null || arrf.equals(null)) {
- Toast.makeText(this, "sd卡里面没有数据", Toast.LENGTH_LONG);
- return 0;
- } else {
- return 1;
- }
- }
- public class MyAdapter extends BaseAdapter {
- public int getCount() {
- // TODO Auto-generated method stub
- Log.e("---getCount", arrf.length + "");
- return arrf.length;
- }
- public Object getItem(int arg0) {
- // TODO Auto-generated method stub
- return arrf[arg0];
- }
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- LayoutInflater inflater = getLayoutInflater();
- View row = inflater.inflate(R.layout.filerowlayout, null);
- tv1 = (TextView) row.findViewById(R.id.tvfile);
- tv1.setText(arrf[position].getName());
- return row;
- }
- }
- }
最后: 希望大家一起讨论! 谢谢!
完整代码:http://115.com/file/dp932rc9#zt_07_storage.rar