android – assets文件夹及其子文件夹中的文件列表

我在Android项目的“assets”文件夹中有一些带有HTML文件的文件夹.我需要从列表中的资产子文件夹中显示这些HTML文件.我已经写了一些关于制作这个列表的代码.

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

使用资产文件夹的根文件夹名称调用listAssetFiles.

    listAssetFiles("root_folder_name_in_assets");

如果根文件夹是资产文件夹,则调用它

    listAssetFiles("");    
上一篇:如何从Android WebView中的远程URL访问本地资产?


下一篇:在Android Studio中创建图像资源