转载请注明出处
adapter
package com.tianlei.test; import java.io.File; import java.text.DecimalFormat; import java.util.LinkedList; import java.util.List; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; public class FileAdapter extends BaseAdapter { public Activity activity; //创建View时必须要提供Context public List<File> list=new LinkedList<File>(); //数据源(文件列表) public String currPath;//当前路径 private Bitmap bmp_folder,bmp_file; public int getCount() { // TODO Auto-generated method stub return list.size(); } public Object getItem(int arg0) { // TODO Auto-generated method stub return list.get(arg0); } public int getItemViewType(int position){ return position; } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(int position, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub View v=View.inflate(activity,R.layout.item,null); TextView Txt_Name=(TextView) v.findViewById(R.id.Txt_Name); TextView Txt_Size=(TextView) v.findViewById(R.id.Txt_Size); ImageView img=(ImageView) v.findViewById(R.id.image_ico); // CheckBox checkBox = (CheckBox)v.findViewById(R.id.button_delete); /*很重要:内容和list通过适配器Adapter进行连接显示。 * 首先将ListView和Adapter进行连接。 * 内容和Adapter连接是通过list.add()和list.get(position) * 获取list中指定位置的元素:将list中的元素显示*/ File f=list.get(position); Txt_Name.setText(f.getName());//文件名字 Txt_Size.setText(setFileSize(getFolderOrFileSize(f)));//文件夹大小 if(f.isDirectory()){ Txt_Size.setText("文件夹"); } if(f.isDirectory()){ img.setImageBitmap(bmp_folder); }else{ img.setImageBitmap(bmp_file); } return v; } public void scanFiles(String path) { list.clear();/*清空list*/ File dir=new File(path);/*指定路径*/ File[] subFiles=dir.listFiles();//把该路径下的所有文件放入一数组,目的是添加到list if(subFiles!=null)/*该路径下有文件*/ for(File f:subFiles) list.add(f);//添加到list并显示该目录下的所有文件 this.notifyDataSetChanged(); currPath=path; } public FileAdapter(Activity activity) { this.activity=activity; bmp_folder=BitmapFactory.decodeResource(activity.getResources(),R.drawable.folder);//文件夹,decodeResource图片解码,source资源,解码为Bitmap类型; bmp_file=BitmapFactory.decodeResource(activity.getResources(),R.drawable.file);//文件 } public static long getFolderOrFileSize(File file) { long size = 0; /*计算文件夹大小:有bug */ /*if(file.isDirectory()){ File[] fileList = file.listFiles(); for(int i=0; i<fileList.length; i++){ if(fileList[i].isDirectory()){ size = size + getFolderOrFileSize(fileList[i]); }else{ size = size + fileList[i].length(); } } }*/ if(file.isFile()) size = file.length(); return size; } /** * 文件大小单位转换 * * @param size * @return */ @SuppressLint("UseValueOf") public String setFileSize(long size) { DecimalFormat df = new DecimalFormat("###.###"); float f = ((float) size / (float) (1024 * 1024));/*判断是否<1M*/ if(size<1024){ return df.format(new Float(size).doubleValue()) + "B"; }else if ( f < 1.0) { float f1 = ((float) size / (float) (1024));//1KB return df.format(new Float(f1).doubleValue()) + "KB"; }else if(f/(float)(1024)<1.0){ return df.format(new Float(f).doubleValue()) + "M"; }else{ return df.format(new Float(f/(float)(1024)).doubleValue()) + "G"; } } }
2.Activity
public class TestFileActivity extends Activity{ /** Called when the activity is first created. */ ListView listView; /*显示数据的列表*/ TextView Txt_Path; private String filepath; private Filter filter; public EditText editText; private long which; private FileAdapter adapter; private int position; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.testfile); listView=(ListView) findViewById(R.id.List_View); Txt_Path=(TextView) findViewById(R.id.Txt_Path); adapter=new FileAdapter(this); listView.setAdapter(adapter);/*通过BaseAdapter下面的getview()方法显示*/ listView.setOnItemClickListener(new ItemClickListener()); //listView.setOnItemLongClickListener(new ItemLongClickListener()); /*显示指定路径下的文件*/ adapter.scanFiles("/sdcard/COM/"); /*为视图(要显示上下文菜单的视图)注册上下文菜单ContextMenu*/ registerForContextMenu(listView); } public class ItemClickListener implements OnItemClickListener{ private File f; public void onItemClick(AdapterView<?> parent, View v, int positiong, long id) { // TODO Auto-generated method stub FileAdapter da=(FileAdapter) listView.getAdapter(); f=da.list.get(positiong); if(f.isDirectory()){ Txt_Path.setText(f.getPath()); da.scanFiles(f.getPath()); } else if(f.isFile()){ android.app.AlertDialog.Builder builder = new AlertDialog.Builder(TestFileActivity.this); builder.setTitle("功能列表:"); builder.setItems(R.array.listFunction, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub String[] s = getResources().getStringArray(R.array.listFunction);//获得资源列表 if(s[which].equals("运行")){ }else if(s[which].equals("删除文件")){ f.delete(); if(adapter != null){ //adapter.notifyDataSetChanged();//实时数据刷新 /*显示指定路径下的文件*/ adapter.scanFiles("/sdcard/COM/"); } }else if(s[which].equals("上传到云端")){ //采用socket } } }); builder.create().show(); } } }
3.item布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/image_ico" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:src="@drawable/folder" android:layout_centerVertical="true" /> <LinearLayout android:id="@+id/linear01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/image_ico" android:orientation="vertical" > <TextView android:id="@+id/Txt_Name" android:layout_width="fill_parent" android:layout_height="20dp" android:text="TextView" /> <TextView android:id="@+id/Txt_Size" android:layout_width="fill_parent" android:layout_height="20dp" android:text="TextView" /> </LinearLayout> <!-- <CheckBox android:id="@+id/button_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:layout_alignParentRight="true"/> --> </RelativeLayout>
4.Activity布布局文件
<?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" android:background="@android:color/white" > <TextView android:id="@+id/Txt_Path" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="25dp" android:text="测试文件列表" /> <ListView android:id="@+id/List_View" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cacheColorHint="#00000000" <!-- 滑动列表时,底部不变色 --> android:fadingEdge="none" > </ListView> </LinearLayout>