文件浏览器,查看文件结构
package zhang.example;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class javalist extends ListActivity {
private List<String> items = null;
private List<String> paths = null;
private String rootpath = "/";
private TextView mpath;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mpath = (TextView) findViewById(R.id.mpath);
getFileDir(rootpath);
}
public void getFileDir(String filePath) {
mpath.setText(filePath);
items = new ArrayList<String>();
paths = new ArrayList<String>();
File f = new File(filePath);
File[] files = f.listFiles();
if (!filePath.equals(rootpath)) {
items.add("Back to" + rootpath);
paths.add(rootpath);
items.add("Back to ../");
paths.add(f.getParent());
}
for (int i = 0; i < files.length; i++) {
File file = files[i];
items.add(file.getName());
paths.add(file.getPath());
}
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this,
R.layout.file, items);
setListAdapter(fileList);
}
public 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 files!")
.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0,
int arg1) {
// TODO Auto-generated method stub
}
}).show();
}
} else {
new AlertDialog.Builder(this).setTitle("Message")
.setMessage("权限不够").setPositiveButton("ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0,
int arg1) {
// TODO Auto-generated method stub
}
}).show();
}
}
}
子项的布局
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:textSize="20sp"/>
主布局
<?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:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/mpath"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>