gpt4 book ai didi

android - 如何列出位于 sd 卡上的所有文件和文件夹

转载 作者:IT王子 更新时间:2023-10-28 23:35:47 26 4
gpt4 key购买 nike

我制作了一个程序,列出了位于 sd 卡上的所有文件和文件夹(f&f)。如果我触摸列表项之一(如果它是一个文件夹)然后列表显示位于该文件夹上的 faf。

这里是源代码

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;
}

}

现在我有两个问题

  1. 当我触摸“返回”按钮时,它会向后列出两步。说目前该列表在 "external_sd/Video/Bangla"下显示 f&f。按下后退按钮后,列表不在“external_sd/Video/”下显示f&f,而是在“external_sd/”下。

  2. 有没有更好的解决方案来显示所有 f&f,如 java 中的 JFileChooser ?

最佳答案

似乎当您触摸 Back dispatchKeyEvent() 时会收到两次 KeyEvent KEYCODE_BACK,所以我建议您这样做:

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 ));


}

关于android - 如何列出位于 sd 卡上的所有文件和文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8638293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com