How to list all files and folders locating on sd card

前端 未结 6 1807
抹茶落季
抹茶落季 2020-12-01 03:14

I have made a program that list all files and folders(f&f) locating on sd card. If i touch one of the list item ( if it is a folder ) then the list shows faf locating on

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 03:54

    It seems that when you touch Back dispatchKeyEvent() receive twice the KeyEvent KEYCODE_BACK, so I suggest you do it this way :

    public class FileList extends ListActivity 
    {
    private File file;
    private List myList;
    
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    
        myList = new ArrayList();   
    
        String root_sd = Environment.getExternalStorageDirectory().toString();
        file = new File( root_sd + "/external_sd" ) ;       
        File list[] = file.listFiles();
    
        for( int i=0; i< list.length; i++)
        {
                myList.add( list[i].getName() );
        }
    
        setListAdapter(new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, myList ));
    
    }
    
    protected void onListItemClick(ListView l, View v, int position, long id) 
    {
        super.onListItemClick(l, v, position, id);
    
        File temp_file = new File( file, myList.get( position ) );  
    
        if( !temp_file.isFile())        
        {
            file = new File( file, myList.get( position ));
            File list[] = file.listFiles();
    
            myList.clear();
    
            for( int i=0; i< list.length; i++)
            {
                myList.add( list[i].getName() );
            }
            Toast.makeText(getApplicationContext(), file.toString(), Toast.LENGTH_LONG).show(); 
            setListAdapter(new ArrayAdapter(this,
                    android.R.layout.simple_list_item_1, myList ));
    
        }
    
    }
    
    
    @Override
    public void onBackPressed() {
                String parent = file.getParent().toString();
                file = new File( parent ) ;         
                File list[] = file.listFiles();
    
                myList.clear();
    
                for( int i=0; i< list.length; i++)
                {
                    myList.add( list[i].getName() );
                }
                Toast.makeText(getApplicationContext(), parent,          Toast.LENGTH_LONG).show(); 
                setListAdapter(new ArrayAdapter(this,
                        android.R.layout.simple_list_item_1, myList ));
    
    
        }
    

提交回复
热议问题