package lee.mylist;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.ListView;
public class MyList extends ListActivity {
private List<String> items = null;
private List<String> paths = null;
private TextView mPath;
private String rootPath="/";
/** 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.TextView01);
getFileDir(rootPath);
}//onCreate
private void getFileDir(String filePath)
{
mPath.setText(filePath);
items=new ArrayList<String>();
paths=new ArrayList<String>();
File f = new File(filePath);
File[] fs = f.listFiles();
if(!filePath.equals(rootPath))
{
items.add("back to root");
paths.add(rootPath);
items.add("back to parent dir");
paths.add(f.getParent());
}
for(int i=0;i<fs.length;i++)
{
File f2 = fs[i];
items.add(f2.getName());
paths.add(f2.getPath());
}
ArrayAdapter<String> mfileList =
new ArrayAdapter<String>(this,R.layout.mytestview,items);
setListAdapter(mfileList);
}
@Override
protected void onListItemClick(ListView l,View v,int position,long id)
{
File mfile = new File(paths.get(position));
if(mfile.isDirectory())
{
try
{
getFileDir(paths.get(position));
}
catch(Exception e){}
}
else
{}
}
}//class
##########################################################
<?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:text="@+id/TextView01"
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<ListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
#########################################################
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:text="@+id/TextView01"
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="30px"
android:textSize="16sp">
</TextView>