用android 获取当前本地文件、文件夹,并显示在listview上,点击文件夹,显示该文件夹下的文件和文件夹?详解
解决方案 »
- TabHost和独立的Activity跳转问题
- 可以android端向WebService端发送一个对象吗?八百里加急,求罩!!!!
- android 如何打印插件中的日志信息
- Bitmap的疑惑
- android中怎样解决540*960分辨率问题?
- 在Activity中启动的service是一个线程还是一个进程
- android自动挂载多个USB设备
- 求助,menu item过多情况下的显示问题
- 为什么点击不起作用呢?
- Android studio怎么将加载的bitmap类型传输到uint8_t *数据类型进行计算
- android TextView显示容量很大的.txt文件
- CheckBoxPreference则么在左方添加小图片?
package com.hundsun.zhoujl.android;import java.io.File;
import java.util.ArrayList;import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;public class Test_fileActivity extends ListActivity { private ArrayList<String> items = null;
private ArrayList<String> paths = null;
private String rootPath = "/";
private TextView mPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPath = (TextView)findViewById(R.id.mPath);
mPath.setTextColor(Color.RED);
getFileDir(rootPath);
}
private void getFileDir(String filePath) {
mPath.setText(filePath);
items = new ArrayList<String>();
paths = new ArrayList<String>();
File file = new File(filePath);
File[] files = file.listFiles();
if(!filePath.equals(rootPath)) {
items.add("Back To " + rootPath);
paths.add(rootPath);
items.add("Back to ../");
paths.add(file.getParent());
}
for(File fileTemp :files) {
items.add(fileTemp.getName());
paths.add(fileTemp.getPath());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Test_fileActivity.this,R.layout.file_now,items);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
File file = new File(paths.get(position));
if(file.canRead()) {
if(file.isDirectory()) {
getFileDir(paths.get(position));
}else {
new AlertDialog.Builder(this)
.setTitle("Message")
.setMessage("["+file.getName() + "] is a file")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
}else {
new AlertDialog.Builder(this)
.setTitle("Message")
.setMessage("權限不足~")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).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"
>
<TextView android:id="@+id/mPath"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>file_now.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="20px"
android:textSize="14sp"
>
</TextView>