如何列出位于 SD 卡上的所有文件和文件夹 [英] How to list all files and folders locating on sd card
问题描述
我制作了一个程序,列出了位于 SD 卡上的所有文件和文件夹(f&f).如果我触摸列表项之一(如果它是一个文件夹)那么列表会显示位于该文件夹上的 faf.
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.
这是源代码
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;
}
}
现在我有两个问题
当我触摸后退"按钮时,它会向后列出两步.说当前列表显示 f&f 下 "external_sd/Video/Bangla" .按后退按钮后,列表没有在external_sd/Video/"下显示 f&f,而是在external_sd/"下.
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/".
是否有更好的解决方案可以在 Java 中显示所有像 JFileChooser 这样的 f&f ?
Is there a better solution to show all f&f like JFileChooser in java ?
推荐答案
看起来当你触摸 Back dispatchKeyEvent() 时会收到两次 KeyEvent KEYCODE_BACK,所以我建议你这样做:
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 ));
}
这篇关于如何列出位于 SD 卡上的所有文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!