如何在Android Studio中获取位于Assets文件夹中的文本文件并显示在ListView中。

huangapple go评论69阅读模式
英文:

how to get text file in listview containing in Assets folder android studio

问题

如何在Android Studio中将Assets文件夹中的文件名显示在RecyclerView中?

我的Assets文件夹中有Apple.txt、Banana.json、cat.xml、Dog.xml、Elephant.java、Fox.json、Got.gradle等文件。
我只想在列表中显示名称和类型,例如Apple.txt。

英文:

How to show assets folder file names into recycler view in Android Studio?

Files in my Assets folder are Apple.txt, Banana.json, cat.xml, Dog.xml, Elephant.java, Fox.json, Got.gradle etc
i want only name with it's type in list like Apple.txt

答案1

得分: 0

你可以通过调用AssetManager提供的API来访问应用程序的原始资源文件。

AssetManager assetManager = context.getAssets();
String[] files = assetManager.list("");  // ""代表资源根文件夹

在获取包含根文件夹内所有资源的字符串数组后,你可以将该数组传递给你的RecyclerView.Adapter,并在onBindViewHolder中将其绑定到你的RecyclerView.ViewHolder上。

英文:

You can access the application's raw asset files by calling APIs provided by AssetManager.

AssetManager assetManager = context.getAssets();
String[] files = assetManager.list("");  // "" means asset root folder

After you get the String array that contains all the assets at the root folder, you can pass the array to your RecyclerView.Adapter and bind it to your RecyclerView.ViewHolder in onBindViewHolder.

答案2

得分: 0

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);
英文:
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);

huangapple
  • 本文由 发表于 2020年9月8日 09:47:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63786120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定