如何在Android上的ArrayList中的每个元素的末尾添加逗号 [英] How to add a comma at the end of every element in ArrayList On Android

查看:16
本文介绍了如何在Android上的ArrayList中的每个元素的末尾添加逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想使用这个 Library 来显示 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 : 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆