android的listactivity实例

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 items = null;
    private List 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();
        paths=new ArrayList();
        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         {
            File f2 = fs[i];
            items.add(f2.getName());
            paths.add(f2.getPath());
        }
       ArrayAdapter mfileList =
           new ArrayAdapter(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

##########################################################


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
android:text="@+id/TextView01"
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">


 

android:layout_width="fill_parent"
android:layout_height="wrap_content">


#########################################################



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">

 

 

你可能感兴趣的:(android,layout,encoding,file,listview,exception)