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

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

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



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值