Reputation: 27
Hello fellow programmers,
In my app I just deserialized an ArrayList from a text file and now I have an ArrayList with Strings. For design purposes I want to put the contents of the ArrayList into one organized string with comas and spaces.
I made an attempt at it but he app forcecloses and the LogCat pinpoints the error to this code:
FYI: OptionsText is an empty string and ListOptions is the ArrayList with the strings.
public void getOptionsText(){
int i;
OptionsText=ListOptions.get(1);
for(i=2; i<ListOptions.size(); i++){
OptionsText = OptionsText + ", " + ListOptions.get(i);
}
Options.setText(OptionsText);
}
Upvotes: 0
Views: 129
Reputation: 7871
Other than the other answers given by fellow SO users, you can also use the join
method from the StringUtils
class from org.apache.commons.lang
.
OR
The join
method from the Joiner
class from com.google.common.base
Upvotes: 0
Reputation: 320
can you please try this...check if list is not null...
list.toString().replace("[", "").replace("]", "");
Upvotes: 0
Reputation: 359786
Just use ArrayList#toString()
:
public void getOptionsText() {
String OptionsText = ListOptions.toString();
Options.setText(OptionsText.substring(1, OptionsText.length-1);
}
Upvotes: 1