Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7006869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:32:00+00:00 2026-05-27T21:32:00+00:00

I have made a program that list all files and folders(f&f) locating on sd

  • 0

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 that folder.

Here is the source code

public class FileList extends ListActivity 
{
private File file;
private List<String> myList;

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    myList = new ArrayList<String>();   

    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<String>(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<String>(this,
                android.R.layout.simple_list_item_1, myList ));

    }

}


@Override
public boolean dispatchKeyEvent(KeyEvent event) 
{
    if( KeyEvent.KEYCODE_BACK == event.getKeyCode())
    {
        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<String>(this,
                android.R.layout.simple_list_item_1, myList ));


    }

    return true;
}   

}

Now i have two questions

  1. when i touch “back” button then it list back two step. say currently the list is showing the f&f under “external_sd/Video/Bangla” .
    After pressing back button the list is not showing f&f under “external_sd/Video/”, but under “external_sd/”.

  2. Is there a better solution to show all f&f like JFileChooser in java ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:32:00+00:00Added an answer on May 27, 2026 at 9:32 pm

    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<String> myList;
    
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    
        myList = new ArrayList<String>();   
    
        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<String>(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<String>(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<String>(this,
                        android.R.layout.simple_list_item_1, myList ));
    
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pointer of a structure type that I made. On program start
I have made a java GUI program and have added a jList on that
Made a program retrieving .txt files from a folder. Each .txt files contains 1
I have made a program that finds and measures the radius of yeast colonies
I have a problem with a small program that i made. Below is the
My C++ is a little rusty but I have made a program that reverses
I have a program that sorts big files by splitting them into chunks, sort
I have made a program in c and wanted to see, how much memory
I have made a registration program. Making use of mysql database. Can I still
I have made my own file type (.ddd) and I made a simple program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.