In my application I want use this Library for show ArrayList
items.
My ArrayList from server:
\"genres\": [
\"Action\",
Can you try this using for loop like this:
EDITED :
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
}
}
The java.util.ArrayList.add(int index, E elemen) method inserts the specified element E at the specified position in this list.It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).
Edited
if (cloudChipList.size() > 0)
{
if(cloudChipList.get(cloudChipList.size()-1).contains(","))
{
infoSerialFrag_GenreChips.add(str);
}
else
{
infoSerialFrag_GenreChips.add(str+",");
}
}
OP will be
Ganre : Action , Comedy , Family
Use TextUtils.join(",", yourList);
Although it is late, just case if someone else comes to this thread... If you are using Java 8, you can use streaming and Collectors interfaces like the following:
List<String> cloudChipList = new ArrayList<String>();
cloudChipList.add("Action");
cloudChipList.add("Comedy");
cloudChipList.add("Family");
String result = cloudChipList.stream().collect(Collectors.joining(" , ", "Genre: ", "\n"));
System.out.println(result);
Here Collectors.joining
adds delimiter, prefix and suffix to the result, but it has an option with only the delimter, too.
Do it the old Java 6 way and add the comma yourself unless its the last element in the list.
See the following example:
private String[] mostlyMatchedKeywordsStrings = serialResponse.getData().getGenres();
private List<String> cloudChipList = new ArrayList<>();
for (int i = 0; i < mostlyMatchedKeywordsStrings.length; i++) {
String str = mostlyMatchedKeywordsStrings[i];
if (i + 1 < mostlyMatchedKeywordsStrings.length) str = str + ", ";
cloudChipList.add(str);
if (cloudChipList.size() > 0) {
infoSerialFrag_GenreChips.addChip(str);
}
}