资产文件夹的文件和子文件夹列表 [英] List of files in assets folder and its subfolders
本文介绍了资产文件夹的文件和子文件夹列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个HTML文件中的某些文件夹中的资产文件夹在我的Android项目。我需要在列表中显示来自资产的子文件夹这些HTML文件。我已经写了一些code有关使这个名单。
LV1 =(ListView控件)findViewById(R.id.listView);
//将数组中的ListView
//在下一行我需要插入的文件名的字符串数组
//所以,请告诉我,如何让这阵
lv1.setAdapter(新ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_1,filel));
lv1.setTextFilterEnabled(真正的);
在ListView中//的onclick项目:
lv1.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>一种,视图V,INT位置,长的id){
//单击项目位置
ItemName字符串=新的整数(位置)的ToString();
意向意图=新的意图();
intent.setClass(DrugList.this,Web.class);
叠B =新包();
//我不知道它在做什么在这里
b.putString(defStrID了itemname);
intent.putExtras(B);
//启动意图
startActivity(意向);
}
});
解决方案
私人布尔listAssetFiles(字符串路径){
的String []列表;
尝试 {
表= getAssets()列表(路径)。
如果(list.length大于0){
//这是一个文件夹
对于(字符串的文件:列表){
如果(!listAssetFiles(路径+/+文件))
返回false;
}
} 其他 {
//这是一个文件
// TODO:添加文件名到一个数组列表
}
}赶上(IOException异常E){
返回false;
}
返回true;
}
调用listAssetFiles您的资源文件夹的根文件夹名称。
listAssetFiles(root_folder_name_in_assets);
如果在根文件夹是资产的文件夹,然后用调用它
listAssetFiles();
I have some folders with HTML files in the "assets" folder in my Android project. I need to show these HTML files from assets' sub-folders in a list. I already wrote some code about making this list.
lv1 = (ListView) findViewById(R.id.listView);
// Insert array in ListView
// In the next row I need to insert an array of strings of file names
// so please, tell me, how to get this array
lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, filel));
lv1.setTextFilterEnabled(true);
// onclick items in ListView:
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//Clicked item position
String itemname = new Integer(position).toString();
Intent intent = new Intent();
intent.setClass(DrugList.this, Web.class);
Bundle b = new Bundle();
//I don't know what it's doing here
b.putString("defStrID", itemname);
intent.putExtras(b);
//start Intent
startActivity(intent);
}
});
解决方案
private boolean listAssetFiles(String path) {
String [] list;
try {
list = getAssets().list(path);
if (list.length > 0) {
// This is a folder
for (String file : list) {
if (!listAssetFiles(path + "/" + file))
return false;
}
} else {
// This is a file
// TODO: add file name to an array list
}
} catch (IOException e) {
return false;
}
return true;
}
Call the listAssetFiles with the root folder name of your asset folder.
listAssetFiles("root_folder_name_in_assets");
If the root folder is the asset folder then call it with
listAssetFiles("");
这篇关于资产文件夹的文件和子文件夹列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文