新手刚学习安卓,写了个程序
根目录是scard,点击文件夹可以浏览其子目录所有文件,
比如点击一个文件夹,怎样实现返回父目录,求教高手主程序代码:
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;public class ExplorerActivity extends ListActivity{   

private String rootPath = "/sdcard";
private String currentPath = "";
private ArrayList<String> paths=new ArrayList<String>();    ListView mListView = null; 
    ArrayList<Map<String,Object>> mData= new ArrayList<Map<String,Object>>();  
    SimpleAdapter adapter = null;
    
    protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState);
    mListView = (ListView)findViewById(R.id.fileList);
    currentPath = rootPath;
    showList();     
    } 
    
    private void showList(){
     mData = getDir(currentPath);
     adapter = new SimpleAdapter(this,mData,R.layout.mylist,  
             new String[]{"image","title"},new int[]{R.id.list_image,R.id.list_title});          
     mListView.setAdapter(adapter);
     mListView.setSelection(0);    
     mListView.setOnItemClickListener(new OnItemClickListener() {  
        @Override  
        public void onItemClick(AdapterView<?> adapterView, View view, int position,  
            long id) {  
         currentPath = paths.get(position);
         File f = new File(currentPath); 
         if(f.isDirectory()){
         fresh(currentPath);
         } else {
         Toast.makeText(ExplorerActivity.this, "this is not a directory", Toast.LENGTH_SHORT).show();
         }
        
        }  
    });
    }
    private void fresh(String path){
     mData.clear();
mData = getDir(path);
adapter.notifyDataSetChanged();
    }    private ArrayList<Map<String, Object>> getDir(String path){  
    mListView = getListView();
    paths = new ArrayList<String>();
    File f = new File(path);
File fileList[] = f.listFiles();

     int length = fileList.length;       for(int i =0; i < length; i++) {  
        Map<String,Object> item = new HashMap<String,Object>();  
        item.put("image", R.drawable.file_icon);  
        item.put("title", fileList[i].getName());
        File file=fileList[i];
paths.add(file.getPath());
        mData.add(item);   
     }          
    
return mData;
    }
}main.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" > <ListView 
    android:id="@+id/fileList"
    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >     
</ListView>
</LinearLayout>mylist.xml<?xml version="1.0" encoding="utf-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
android:fastScrollEnabled="true" 
    android:layout_width="fill_parent" 
    android:layout_height="40dp">      <ImageView 
        android:id="@+id/list_image"  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"   
        />  
    <TextView 
        android:id="@+id/list_title"  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"  
        android:layout_toRightOf="@+id/list_image" 
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical"     
        android:textSize="20sp" 
        android:singleLine="true"
        />   
</RelativeLayout>  

解决方案 »

  1.   

    ArrayList方法改一下:private ArrayList<Map<String, Object>> getDir(String path)
    {
    mListView = getListView();
    paths = new ArrayList<String>();
    File f = new File(path);
    File fileList[] = f.listFiles();
    int length = fileList.length;

    if(length<1)
    {
    if(f.getParent()!=null)
    {
    Map<String,Object> item1 = new HashMap<String,Object>();
    item1.put("image", R.drawable.file_icon);
    item1.put("title", "..");
    mData.add(item1);
    paths.add(f.getParent());
    }
    }
    else
    {
    for(int i =0; i < length; i++)
    {
    File file=fileList[i];
    if(i==0)
    {
    if(f.getParent()!=null)
    {
    Map<String,Object> item1 = new HashMap<String,Object>();
    item1.put("image", R.drawable.file_icon);
    item1.put("title", "..");
    mData.add(item1);
    paths.add(f.getParent());
    }
    }
    paths.add(file.getPath());
    Map<String,Object> item = new HashMap<String,Object>();
    if(file.isDirectory())
    {
    item.put("image", R.drawable.file_icon);
    }
    else
    {
    item.put("image", R.drawable.file_icon);
    }
    item.put("title", fileList[i].getName());
    mData.add(item);
    }
    }
    return mData;
    }