Question

I know this is probably one of dumbest questions ever but my brain seems off. I have this method which makes a string out of vCard:

public static String process(String vCard) {
        ArrayList<ArrayList<String>> vCardData = parseData(vCard);
        if (vCardData != null) {
            StringBuilder readableVCard = new StringBuilder();

            for (int i = 0; i < FIELD_COUNT; i++) {
                ArrayList<String> vCardDataField = vCardData.get(i);

                if (vCardDataField.size() > 0) {
                    String field = null;

                    if (i == FORMATTED_NAME) {
                        field = "Name: ";
                    } else if (i == PHONE_MOBILE) {
                        field = "Phone (mobile): ";
                    } else if (i == PHONE_HOME) {
                        field = "Phone (home): ";
                    } else if (i == PHONE_WORK) {
                        field = "Phone (work): ";
                    } else if (i == PHONE) {
                        field = "Phone: ";
                    } else if (i == FAX_HOME) {
                        field = "Fax (home): ";
                    } else if (i == FAX_WORK) {
                        field = "Fax (work): ";
                    } else if (i == PAGER) {
                        field = "Pager: ";
                    } else if (i == EMAIL_HOME) {
                        field = "Email (home): ";
                    } else if (i == EMAIL_WORK) {
                        field = "Email (work): ";
                    } else if (i == EMAIL) {
                        field = "Email: ";
                    } else if (i == ORGANISATION) {
                        field = "Company: ";
                    } else if (i == JOB_TITLE) {
                        field = "Job title: ";
                    } else if (i == ADDRESS_HOME) {
                        field = "Address (home): ";
                    } else if (i == ADDRESS_WORK) {
                        field = "Address (work): ";
                    } else if (i == ADDRESS) {
                        field = "Address: ";
                    } else if (i == IM_SKYPE) {
                        field = "Skype: ";
                    } else if (i == IM_GOOGLE) {
                        field = "Google Talk: ";
                    } else if (i == IM_JABBER) {
                        field = "Jabber: ";
                    } else if (i == IM_YAHOO) {
                        field = "Yahoo: ";
                    } else if (i == IM_MSN) {
                        field = "MSN: ";
                    } else if (i == IM_ICQ) {
                        field = "ICQ: ";
                    } else if (i == IM_AIM) {
                        field = "AIM: ";
                    } else if (i == TWITTER) {
                        field = "Twitter: ";
                    } else if (i == BIRTHDAY) {
                        field = "Birthday: ";
                    } else if (i == ANNIVERSARY) {
                        field = "Anniversary: ";
                    } else if (i == NOTES) {
                        field = "Notes: ";
                    } else if (i == WEBSITE) {
                        field = "Website: ";
                    } else {
                        continue;
                    }

                    if (readableVCard.length() != 0) {
                        readableVCard.append("\n");
                    }
                    readableVCard.append(field);

                    for (int j = 0; j < vCardDataField.size(); j++) {
                        if (j != 0) {
                            readableVCard.append("; ");
                        }
                        readableVCard.append(vCardDataField.get(j));
                    }
                }
            }

            if (readableVCard.length() != 0) {
                String textVCard = readableVCard.toString();
                try {
                    textVCard = qpDecoder.decode(readableVCard.toString());
                } catch (Exception e) {
                    Logger.e("VCard to UTF-8", e.getMessage());
                }

                return (textVCard);
            }
        }

        return (null);
    }

So my current output is like this:

Name: Marko
Phone(mobile):1312
Phone(fax):441231
Phone(home):543534
Email(home):dddd
Email(work):eeee
Email(other):aaaa

What I want is to add a line break between groups (name/phone/email) so I get something like this:

Name: Marko

Phone(mobile):1312
Phone(fax):441231
Phone(home):543534

Email(home):dddd
Email(work):eeee
Email(other):aaaa

Addressblablabla
.
.
.

IMsblablabla
.
.
.

Can someone help please? Thanks.

Was it helpful?

Solution

You could iterate the string array, splitting by : and then checking if next item starts with current. If it does, then do nothing, if it doesn't you add the line break to current item and replace it in the array.

Note: splitting with : also separates all phone (xxx) types. To avoid this, you can create enum type, map type to Phone if the string starts with "phone" and use that enum as current type while iterating through the array. If current type if different than next type you add line break.

OTHER TIPS

You can append line separator via System.getProperty("line.separator");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top