问题描述
如何在Android Studio中的资源回收器视图中显示资产文件夹文件名?
我的Assets文件夹中的文件是Apple.txt,Banana.json,cat.xml,Dog.xml,Elephant.java,Fox.json,Got.gradle等 我只想输入名称,例如Apple.txt
解决方法
您可以通过调用AssetManager
提供的API来访问应用程序的原始资产文件。
AssetManager assetManager = context.getAssets();
String[] files = assetManager.list(""); // "" means asset root folder
在包含根文件夹中所有资产的String数组之后,可以将该数组传递到RecyclerView.Adapter
并将其绑定到RecyclerView.ViewHolder
中的onBindViewHolder
。
AssetManager assetManager = getApplicationContext().getAssets();
String[] files = new String[0]; // "" means asset root folder
try {
files = assetManager.list("activity");
} catch (IOException e) {
e.printStackTrace();
}
RecyclerView.Adapter adapter = new cvRecyclerAdapter(this,Arrays.asList(files));
chipsText.setAdapter(adapter);