在我的应用程序中,我想使用这个 Library用于显示 ArrayList
项。
来自服务器的我的 ArrayList:
"genres": [
"Action",
" Comedy",
" Family"
]
我为显示项目编写了以下代码:
private String[] mostlyMatchedKeywordsStrings = serialResponse.getData().getGenres();
private List<String> cloudChipList = new ArrayList<>();
for (String str : mostlyMatchedKeywordsStrings) {
cloudChipList.add(str);
if (cloudChipList.size() > 0) {
infoSerialFrag_GenreChips.addChip(str);
}
}
给出的输出为:
Ganre : Action Comedy Family
但我想要这样:
Ganre : Action , Comedy , Family
请帮助我完成上述代码,我是业余爱好者,需要此帮助,请帮助我完成上述代码。谢谢大家<3
最佳答案
你能像这样使用 for 循环试试这个吗:
已编辑:
private String[] mostlyMatchedKeywordsStrings;
private List<String> cloudChipList = new ArrayList<>();
mostlyMatchedKeywordsStrings = serialResponse.getData().getGenres();
for (String str : mostlyMatchedKeywordsStrings) {
cloudChipList.add(str);
}
if (cloudChipList.size() > 0) {
for (int i = 0; i < cloudChipList.size(); i++) {
if (i == (cloudChipList.size() - 1)) //true only for last element
infoSerialFrag_GenreChips.add(cloudChipList.get(i));
else
infoSerialFrag_GenreChips.add(cloudChipList.get(i) + ","); //this will execute for 1st to 2nd last element
}
}
关于android - 如何在 Android 上的 ArrayList 中的每个元素的末尾添加一个逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45518655/