如何在Android上ArrayList中每个元素的末尾添加逗号 [英] How to add a comma at the end of every element in ArrayList On Android
本文介绍了如何在Android上ArrayList中每个元素的末尾添加逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我想使用此库来显示ArrayList
项目.
我来自服务器的ArrayList:
In my application I want use this Library for show ArrayList
items.
My ArrayList from server:
"genres": [
"Action",
" Comedy",
" Family"
]
我为表演物品写以下代码:
I write below code for show Items:
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:动作喜剧大家庭
Ganre : Action Comedy Family
但是我想要这样:
Ganre:动作,喜剧,家庭
Ganre : Action , Comedy , Family
请帮助我提供我的上述代码,我是业余爱好者,需要此帮助,请帮助我提供我的上述代码.谢谢所有< 3
Please help me with my above codes, I am amateur and need this help, please help me with my above codes. Thanks all <3
推荐答案
您可以使用for循环这样尝试吗:
Can you try this using for loop like this:
已
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上ArrayList中每个元素的末尾添加逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文