1

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);
    }
}

Gives oputput as:

Ganre : Action Comedy Family

But I want it like this :

Ganre : Action , Comedy , Family

Please help me with my above codes, I am amateur and need this help, please help me with my above codes. Thanks all <3

1
  • Suggestion don't save ',' in array list instead get the genre and append ',' at the end. Also put check whether the element is last to prevent last ',' to be printed Commented Aug 5, 2017 at 5:45

5 Answers 5

3

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
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

can you edit your code with my above codes? please send to me full code with my above codes. please I really need your help
In which arraylist you want that colon separated strings? In cloudChipList or in infoSerialFrag_GenreChips?
infoSerialFrag_GenreChips, please help me my friend. I am amateur and really need your helps. please
Jong, your requirements are not clear so not getting exactly what you want. Hope my edited answer helps you.
2

Use TextUtils.join(",", yourList);

1 Comment

can you edit your code and send to me full code? please my friend
0

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

7 Comments

if use this, show me this : Ganre : Action , Comedy , Family , !!!! I don't want show , to lasted of list
@Jong get last value infoSerialFrag_GenreChips.get(infoSerialFrag_GenreChips.size()-1); Then remove (String remove) ,
where add your code? can you edit your answer and send to me full code with my above codes ? please man, I really need your help. please
I showing this in my request response! I edit my code with your help, but show me such as this : Action , Comedy , Family , . I want Remove lasted , and show me like this : Action , Comedy , Family. how can edit my code? please help me my friend
|
0

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);
    }
}

1 Comment

if use this, show me this : Ganre : Action , Comedy , Family , !!!! I don't want show , to lasted of list
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.