2

I have a string array emails and a string variable Category_name. To get and set the results for Category_name i use this:

holder.category_name.setText(mcategoryset.get(position).getCategory_name());

but for emails which is a string array , i am doing this:

holder.category_emails.setText(mcategoryset.get(position).getEmails().get(0));

i.e. i am getting value of position 0 only. How can i get all the values programatically instead of getting just a single value?

1
  • For android development, you can use TextUtils lib as holder.category_emails.setText(TextUtils.join(",",mcategoryset.get(position).getEmails())); Commented Mar 20, 2017 at 9:09

5 Answers 5

2

Use below code

String emails = "";

for(int i = 0; i<mcategoryset.get(position).getEmails().size();i++){
 emails = emails+mcategoryset.get(position).getEmails().get(i)+",";
}

holder.category_emails.setText(emails);
Sign up to request clarification or add additional context in comments.

Comments

1

Try this :

final StringBuilder stringBuilder = new StringBuilder();

for (String email : categorySet.get(position).getEmails()) {
    if (!stringBuilder.toString().isEmpty()) {
        stringBuilder.append("\n");
    }
    stringBuilder.append(email);
}

holder.category_emails.setText(stringBuilder.toString());

1 Comment

Sir, i am getting ',' after end of values too. What can i do to remove comma at the end?
0

You can use try using java.util.Arrays which is a utility class method provided by java to ease operations like these. Try doing something like this :-

holder.category_emails.setText(Arrays.toString(mcategoryset.get(position).getEmails()));

Other options are :-

commons-lang also have that - ArrayUtils.toString(array) (but prefer the JDK one)

commons-lang allows for custom separator - StringUtils.join(array, ',')

Guava also allows a separator, and has the option to skip null values: Joiner.on(',').skipNulls().join(array)

Comments

0

I think you should overload your function, like category_emails.setText(String[]) or use for-each to go through the value.

Comments

0

In your POJO class, for email getter add another getter, something like this,

public String getFullEmails(){
    StringBuilder emailString = new StringBuilder();
    int length = emails.length();
    for(for int=0; i<length; i++){
        emailString.append(emails[i]);
        if(i != length-1){
            emailString.append(",\n");
        }
    }
    return emailString.toString();
}

This method of writing the logic in POJO class other than writing it in Adapter directly will provide you cleaner code and you can work on Actual adapter logic in Adapter rather than writing same Email code again and again.

4 Comments

Use StringBuilder instead. Remember that strings are immutable in Java. You are creating new ones at every +
@Edd, StringBuilder and String concatenations have negligible impact on performance while performing small string concatenations. Definitely StringBuilders are better for heavy task, but for getting 1 to 20 emails, String concatenation is more readable and easy to manage.
I haven't benchmarked it but for sure, for small tasks like this, it will not make a huge difference as you say. Still, it would be good to make readers aware of StringBuilder. I think your answer would be more beneficial to general readers that way.
I recently came to know about a bug in using Concatenation in loops. So I updated my answer long back. Thanks for pointing it out. @Edd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.