主类
directory_list.xml
file_row.xml
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FileList extends ListActivity {
private List<String> items = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.directory_list);
fillWithRoot();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
int selectionRowID = (int) getSelectedItemId();
if (selectionRowID == 0) {
fillWithRoot();
} else {
File file = new File(items.get(selectionRowID));
if (file.isDirectory())
fill(file.listFiles());
else {
Dialog dialog = new AlertDialog.Builder(this).setIcon(android.R.drawable.btn_star).setTitle("Not a Directory").setMessage(
"That's a file, not a directory").setPositiveButton("Cancel", null).create();
dialog.show();
}
}
}
private void fillWithRoot() {
fill(new File("/").listFiles());
}
private void fill(File[] files) {
items = new ArrayList<String>();
items.add(getString(R.string.to_top));
for (File file : files)
items.add(file.getPath());
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.file_row, items);
setListAdapter(fileList);
}
}
directory_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ListView android:id="@id/android:list" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@id/android:empty" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/no_files" />
</LinearLayout>
file_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1" android:layout_width="wrap_content"
android:layout_height="wrap_content" />