读书人

遍历sdcard的资料用listview显示

发布时间: 2012-06-26 10:04:13 作者: rapoo

遍历sdcard的文件,用listview显示


package com.scope.view;


import java.io.File;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


import com.scope.R;

import com.scope.util.FileOp;


import android.app.Activity;

import android.app.LauncherActivity;

import android.app.ListActivity;

import android.graphics.Path;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.widget.AbsListView;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.BaseAdapter;

import android.widget.ListAdapter;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.TextView;


public class FileManagerActivity extends Activity {

private TextView file_path;

private ListView lv;

File path = Environment.getExternalStorageDirectory();

String root_path = path.getAbsolutePath();

List<Map<String, Object>> list;

Map<String, Object> map;

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.main);


file_path = (TextView) findViewById(R.id.file_path);

file_path.setText(root_path);


lv = (ListView) findViewById(R.id.list);

lv.setAdapter(getDeviceListAdapter(root_path));

lv.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> parent, View view,

int pos, long id) {

Map<String, Object> ?item = (Map<String, Object>) parent.getItemAtPosition(pos);

String file_abs_path = (String) item.get("file_abs_path");

System.out.println("file_abs_path ? " + file_abs_path);

File file = new File(file_abs_path);

if (!file.exists()) {

// finish();

return;

}

if(file.isDirectory()){

System.out.println("file_abs_pat" + file_abs_path);

lv.setAdapter(getDeviceListAdapter(file_abs_path));

file_path.setText(file_abs_path);

}else{

}

}

});

}

private List<Map<String, Object>> getFile(String file_path) {

list = new ArrayList<Map<String, Object>>();

File file = new File(file_path);

for (File files : file.listFiles()) {

map = new HashMap<String, Object>();

String file_abs_path = files.getAbsolutePath();

map.put("file_abs_path", file_abs_path);

if (files.isDirectory()) {

map.put("item_type", R.drawable.item_type_dir);

map.put("item_name", files.getName());

map.put("item_sel", R.drawable.item_img_nosel);

long file_date = files.lastModified();

String date = new SimpleDateFormat("yyyy/MM/dd HH:mm")

.format(new Date(file_date));

map.put("item_date", date + " | ");

//long file_size = file.length();

//map.put("item_size", file_size + " | ");

list.add(map);

} else {

map.put("item_name", files.getName());

map.put("item_type", FileOp.getFileTypeImg(files.getName()));

map.put("item_sel", R.drawable.item_img_nosel);

long file_date = files.lastModified();

String date = new SimpleDateFormat("yyyy/MM/dd HH:mm")

.format(new Date(file_date));

map.put("item_date", date + " | ");

long file_size = files.length();

map.put("item_size", FileOp.getFileSizeStr(file_size)+ " | ");

list.add(map);

}

}

return list;

}


private ListAdapter getDeviceListAdapter(String file_path) {

return new SimpleAdapter(FileManagerActivity.this, getFile(file_path),

R.layout.filelist_item, new String[] { "item_type",

"item_name", "item_sel", "item_size", "item_date" },

new int[] { R.id.item_type, R.id.item_name, R.id.item_sel,

R.id.item_size, R.id.item_date });

}

}

读书人网 >移动开发

热点推荐