将字符串的 Arraylist 转换为一个逗号分隔的字符串 [英] Arraylist of strings into one comma separated string

查看:38
本文介绍了将字符串的 Arraylist 转换为一个逗号分隔的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将字符串的 Arraylist 转换为一个大的逗号分隔字符串.

Trying to convert a Arraylist of strings into one big comma separated string.

但是当我使用

String joined = TextUtils.join(", ", participants);

调试器向我显示参与者的大小为 4,但加入的值为",因此为空

Debugger shows me size of 4 for participants however the joined value as "" therefore empty

private ArrayList<String> participants;

不知道出了什么问题?

更新:

List<String> list = new ArrayList<>();
list.add("Philip");
list.add("Paul Smith");
list.add("Raja");
list.add("Ez");

String s = TextUtils.join(", ", list);

当我有一个手动填充的列表时,这会起作用,但是下面是代码现在的工作方式.

This works when I have a list that I manually populate however below is how the code is working right now.

在 onCreate()

In the onCreate()

callApi(type);
String s = TextUtils.join(", ", participants);
getSupportActionBar().setTitle(s);

在 callAPI() 中:

In callAPI():

JSONArray participantsR = sub.getJSONArray("referralParticipants");

Log.e("Participants length ", String.valueOf(participantsR.length()));

for (int i = 0; i < participantsR.length(); i++)
{
    JSONObject object = participantsR.getJSONObject(i);
    String firstname = (String) object.get("fullName");
    participants.add(firstname);
    Log.e("Times", String.valueOf(i));
}

推荐答案

我正在尝试重现您的错误,但无法重现.这是我的代码:

I'm trying to reproduce your error and am unable to. Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_temp);

    List<String> list = new ArrayList<>();
    list.add("Philip Johnson");
    list.add("Paul Smith");
    list.add("Raja P");
    list.add("Ezhu Malai");

    String s = TextUtils.join(", ", list);

    Log.d(LOGTAG, s);
}

我的输出是 Philip Johnson、Paul Smith、Raja P、Ezhu Malai 正如预期的那样.

My output is Philip Johnson, Paul Smith, Raja P, Ezhu Malai as expected.

您是否导入了正确的 TextUtils 类?

Are you importing the correct TextUtils class?

android.text.TextUtils;

鉴于新信息,这是我的方法:

Given the new information, here is my approach:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_temp);

    callApi(type, new OnResponseListener<List<String>>() {
        @Override public void onResponse(List<String> list) {
            getSupportActionBar().setTitle(TextUtils.join(", ", list));
        }
    });
}

我不知道您使用的是什么网络库,但您可能必须将 OnResponseListener 定义为接口.很简单:

I don't know what networking library you're using, but you may have to define OnResponseListener as an interface. It's very easy:

public interface OnResponseListener<T> {
    public void onResponse(T response);
}

然后,您需要修改 callApi 函数以获取 OnResponseListener> 的实例,并在完成调用后调用它的 onResponse 方法.

You will then need to modify your callApi function to take an instance of OnResponseListener> and call it's onResponse method after completing the call.

我建议查看Volley library,并阅读Android 文档关于简单网络调用.

I would recommend looking into the Volley library, and reading the Android documentation about simple network calls.

这篇关于将字符串的 Arraylist 转换为一个逗号分隔的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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