Reputation: 1
I send an arraylist to from my main activity to my second activity with the following code
ArrayList<String> RecipeNames = new ArrayList<>();
RecipeNames.add("Cake");
intentLoadNewActivity.putExtra("names",RecipeNames);
startActivity(intentLoadNewActivity);
And here in my second activity i get the arraylist.
ArrayList<String> names = (ArrayList<String>) getIntent().getStringArrayListExtra("names");
TextView Tname = findViewById(R.id.RecName);
// here I set a textView to be the string that arraylist contains.
Tname.setText(String.valueOf(names));
My problem is when I display the array I get "[Cake]" instead of "Cake"
How do I remove "[]"?
Upvotes: 0
Views: 33
Reputation: 24231
If you just want to remove the braces, I think the following should serve your purpose.
Tname.setText(String.valueOf(names)
.replaceAll("\\[", "").replaceAll("\\]",""));
Upvotes: 0
Reputation: 2859
If you need to format the text before showing it on the screen you can use StringBuilder
Example:
StringBuilder stringBuilder = new StringBuilder();
for (String name : names) {
stringBuilder.append(name);
stringBuilder.append(" ");
}
textView.setText(stringBuilder.toString());
Upvotes: 0
Reputation: 5246
You have a List of String objects but you're trying to display a single one when you're actually displaying multiple. If you want to display a single one, don't pass a List of String, just a String.
If you want to simply get a single String from the List of String objects you can do so with the following.
String anyName = names.stream().findAny().orElse(null);
if (anyName != null) {
// call setText
}
Upvotes: 1