android——文件目录选择

来源:互联网 发布:手机淘宝论坛怎么找 编辑:程序博客网 时间:2024/06/03 21:31


主活动的布局文件   activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/filedire_button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="选择文件" />

    <TextView
        android:id="@+id/filedire_textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text=" " />

</LinearLayout>


显示图片如下



创建适配器所需的布局文件 adapter.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/adapter_listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/adapter_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.48"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/adapter_filename"
            android:layout_width="242dp"
            android:layout_height="match_parent"
            android:text="TextView" />
    </LinearLayout>

</LinearLayout>


主活动程序 MainActivity.java

package com.example.filedirec;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {
    private Button chooseFile;
    private TextView fileDire;
    private String curPath = new String("/");
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        chooseFile = (Button)findViewById(R.id.filedire_button1);
        fileDire = (TextView)findViewById(R.id.filedire_textView1);
        
        chooseFile.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                openCurDir(curPath);
            }
        });
    }
    
    private void openCurDir(String curPath){
        File f = new File(curPath);
        File[] file = f.listFiles();
        final List<Map<String,Object>> listItem = new ArrayList<Map<String,Object>>();

        if(!curPath.equals("/")){//如果不是根目录的话就在要显示的列表中加入此项
            Map<String,Object> map1=new HashMap<String,Object>();
            map1.put("name", "返回上一级目录");
            map1.put("image", R.drawable.back02);
            map1.put("path",f.getParent());
            map1.put("isDire", true);
            listItem.add(map1);
        }
        
        if(file != null){//必须判断 否则目录为空的时候会报错
            for(int i = 0; i < file.length; i++){
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("name", file[i].getName());
                map.put("image", (file[i].isDirectory()) ? R.drawable.folder : R.drawable.doc);
                map.put("path",file[i].getPath());
                map.put("isDire", file[i].isDirectory());
                listItem.add(map);
            }
        }
        
        SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,listItem,R.layout.adapter,
                new String[]{"name","image"},new int[]{R.id.adapter_filename,R.id.adapter_image});
        
        final AlertDialog.Builder b =new Builder(MainActivity.this);
        b.setAdapter(adapter, new DialogInterface.OnClickListener() {
            
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                if((Boolean) listItem.get(arg1).get("isDire")){
                    openCurDir((String)listItem.get(arg1).get("path"));
                }else{
                    fileDire.setText((String)listItem.get(arg1).get("path"));
                }
            }
        });
        b.show();
    }

}


对话框显示如下















0 0
原创粉丝点击