3468

Arraylist of strings into one comma separated string

Question:

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

However when I use the

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

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

<a href="https://i.stack.imgur.com/GqIzL.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/GqIzL.png" data-original="https://i.stack.imgur.com/GqIzL.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

private ArrayList<String> participants;

Not sure what is going wrong?

<a href="https://i.stack.imgur.com/UgVO1.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/UgVO1.png" data-original="https://i.stack.imgur.com/UgVO1.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

UPDATE:

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.

In the onCreate()

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

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

Answer1:

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

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

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

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

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.

I would recommend looking into the <a href="https://developer.android.com/training/volley/index.html" rel="nofollow">Volley library</a>, and reading the <a href="https://developer.android.com/training/volley/simple.html" rel="nofollow">Android documentation</a> about simple network calls.

Answer2:

I don't know what TextUtils does. This will do it.

StringBuffer sb = new StringBuffer(); for (String x : participants) { sb.append(x); sb.append(", "); } return sb.toString();

Easy enough, just use that.

Answer3:

I use <a href="http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringUtils.html" rel="nofollow">StringUtils.join</a> from Apache Common Utilities.

The code is super-simple just the way you wanted,

StringUtils.join(participants,", ");

Works flawlessly for me.

<strong>EDIT</strong>

As requested, here is the <a href="https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/StringUtils.java" rel="nofollow">StringUtils.java</a> file for those who just want to use this single utility class and not the entire library.

Recommend

  • How to insert table and bookmark into Word, one per page
  • Error replacement has 1 row, data has 0 in r shiny app
  • HTML - How do I generate a progress bar circle with a percentage in and set the value with JS or jQu
  • Uniform way to add multiple descriptive properties in Schema
  • MPI Bcast with MPI_Type_create_struct segmentation fault
  • PHP and htaccess redirection: Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
  • Creating an interactive events scheduler
  • In TestCafe is there a way to know if the test passed or failed in after hook?
  • Container of Different Functors
  • How do you suppress GCC linker warnings?
  • Renaming a file after IF EXIST check within ++ FOR loop in a Windows batch file?
  • JSF datatable hide column border if column is not displayed(rendered)
  • XCUITest: Running tests across two apps in a coordinated fashion
  • Upload video and extract thumbnail using Amazon s3
  • Rebuilding again and again to view the imported QML file changes
  • CORS issue on a Delphi's Datasnap ISAPI Module
  • Eclipse fails to build Java Card sample projects
  • How to gracefully stop python unittest?
  • Laravel 5.7: Custom blade template for Maintenance Mode but not 503.blade.php
  • Making query to find nearest multiple(Lat,Long) from the single(Lat,Long)
  • jqGrid Filter Toolbar and Date and Select filters
  • create circular Auto Horizontal Scroll View?
  • characters not allowed in DOM ids by spec, and by browser
  • How to include WYSIWYG text editor in an HTML using google script
  • How to display content depending on dropdown menue user selection
  • Set initial vuetify v-select value
  • Building JavaFX 2.0 App on Mac, deploying on Windows
  • How to use array in autohotkey?
  • Generate and export point cloud from Project Tango
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • How to turn off notice reporting in xampp?