27、fileBrowser 文件和目录浏览

-----------------------main.java----------------------


package com.example.filebrowser;




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.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
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 MainActivity extends ActionBarActivity implements OnItemClickListener {


private static final int IM_PARENT = Menu.FIRST + 1;
private static final int IM_BACK = IM_PARENT + 1;
 
ListView itemlist = null;
String path = "/";
List<Map<String, Object>> list;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("文件浏览器");
itemlist = (ListView) findViewById(R.id.itemlist);
refreshListItems(path);
}


private void refreshListItems(String path) {
setTitle("文件浏览器 > "+path);
list = buildListForSimpleAdapter(path);
SimpleAdapter notes = new SimpleAdapter(this, list, R.layout.file_row,
new String[] { "name", "path" ,"img"}, new int[] { R.id.name,
R.id.desc ,R.id.img});
itemlist.setAdapter(notes);
itemlist.setOnItemClickListener(this);
itemlist.setSelection(0);
}


private List<Map<String, Object>> buildListForSimpleAdapter(String path) {
File[] files = new File(path).listFiles();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(files.length);
Map<String, Object> root = new HashMap<String, Object>();
root.put("name", "/");
root.put("img", R.drawable.file_root);
root.put("path", "go to root directory");
list.add(root);
Map<String, Object> pmap = new HashMap<String, Object>();
pmap.put("name", "..");
pmap.put("img", R.drawable.file_paranet);
pmap.put("path", "go to paranet Directory");
list.add(pmap);
for (File file : files){
Map<String, Object> map = new HashMap<String, Object>();
if(file.isDirectory()){
map.put("img", R.drawable.directory);
}else{
map.put("img", R.drawable.file_doc);
}
map.put("name", file.getName());
map.put("path", file.getPath());
list.add(map);
}
return list;
}

private void goToParent() {
File file = new File(path);
File str_pa = file.getParentFile();
if(str_pa == null){
Toast.makeText(MainActivity.this,"当前根目录了",Toast.LENGTH_SHORT).show();
refreshListItems(path);
}else{
path = str_pa.getAbsolutePath();
refreshListItems(path);
}
}


@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if (position == 0) {
path = "/";
refreshListItems(path);
}else if(position == 1){
goToParent();
} else {
path = (String) list.get(position).get("path");
File file = new File(path);
if (file.isDirectory())
refreshListItems(path);
else
Toast.makeText(MainActivity.this,"当前最子目录",Toast.LENGTH_SHORT).show();
}


}
}


.。。。。。。。。。。main.xml。。。。。。。。。。。。。。。


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


<ListView 
   android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:id="@+id/itemlist" />
</LinearLayout>


.。。。。。。。。。。。。。/res/layout/file_row.xml。。。。。。。。。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vw1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="4px"
    android:orientation="horizontal">    


    <ImageView android:id="@+id/img"
        android:layout_width="32px"
        android:layout_margin="4px"
        android:layout_height="32px"/>
 
   <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <TextView android:id="@+id/name"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>


        <TextView android:id="@+id/desc"
            android:textSize="14sp"
            android:layout_width="fill_parent"
            android:paddingLeft="10px"
            android:layout_height="wrap_content"/>


    </LinearLayout>


</LinearLayout>


、、、、、、注意要加的权限!!!!!!!!、、、


<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

<!-- 因为版本比较新,所以要加下面的权限(因为要访问/sdcard目录)-->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值