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
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/".
Is there a better solution to show all f&f like JFileChooser in java ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…