正确显示联系信息

Properly display contact information

我正在制作一个 Android 应用程序,我会在其中显示联系信息。此信息来自 webAPI,格式为 JSON。我得到的是一堆字符串,如名字、姓氏、phone 号码等

目前我有这样的设置:

JSONObject object = array.getJSONObject(0);
TextView contact = (TextView) view.findViewById(R.id.contact);
String contactString = "";
contactString += object.getString("Voorletters") + " ";
contactString += object.getString("Voorvoegsel").trim() + " ";
contactString += object.getString("Achternaam") + "\n";
contactString += object.getString("Email")+"\n";
if (!object.getString("Mobiel").isEmpty()) {
    contactString += object.getString("Mobiel") + "\n";
}
if (!object.getString("Mobiel2").isEmpty()) {
    contactString += object.getString("Mobiel2") + "\n";
}
if (!object.getString("Telefoon").isEmpty()) {
    contactString += object.getString("Telefoon") + "\n";
}
if (!object.getString("Telefoon2").isEmpty()) {
    contactString += object.getString("Telefoon2") + "\n";
}
contact.setText(contactString);
Linkify.addLinks(contact,Linkify.ALL);

这很好用,您可以点按电子邮件和 phone 号码就好了。我唯一的问题是,当您点击 phone 号码时,我的平板电脑会弹出一个仅显示 phone 号码的弹出窗口,以及一个将 phone 号码添加到联系人列表的选项。

我想要实现的是让 android 看到此信息已链接。这不仅仅是显示 phone 数字,任何时候你点击文本视图中的任何地方,都会弹出一个显示所有联系信息和选项 "add to contacts" 的弹出窗口,最好是

这可能吗?如果是,怎么做?

我建议您考虑使用 Gson class 来有效地解析您的 JSON 对象。它会是这样的,

Gson gson = new Gson();
JSONObject object = array.getJSONObject(0);    
ContactClass person = gson.fromJson((String)object, ContactClass.class); 

Gson 将映射来自 json 对象的所有传入联系人。 之后,我建议您创建一个自定义适配器来以您想要的方式显示联系信息。 我认为没有显示联系人的模板,因此您必须创建自己的适配器和 xml 视图。

您可以使用 Intents.Insert.ACTION,它将使用必要的数据调用联系人应用程序。您需要做的就是添加所需的数据,例如 Phone 号码、电子邮件等作为 intent extra。

// Creates a new Intent to insert a contact
Intent intent = new Intent(Intents.Insert.ACTION);
// Sets the MIME type to match the Contacts Provider
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);

// Inserts a name
intent.putExtra(Intents.Insert.NAME, name);

// Inserts an email address
intent.putExtra(Intents.Insert.EMAIL, mEmailAddress.getText());

startActivity(intent);

更多信息

Insert a Contact using Intent

Intent.Inserts

感谢 iago,我设法找到了解决方案。显然不可能使用默认联系人应用程序来显示自定义信息,但可以创建联系人并显示该联系人。所以我所做的是:

我制作了一个带有联系人 class 的小型图书馆。此联系人 class 包含我希望我的联系人拥有的所有信息。在那里,我创建了一个函数来检查提供的电子邮件地址是否存在于联系人列表中。如果是这样,此功能将启动显示联系信息的意图。否则,它会启动一个意图,允许用户将联系人添加到他们的联系人列表中。完整的 class 看起来像这样:

package nl.buroboot.danielvandenberg.contact;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;

import java.util.ArrayList;

/**
 * @author Daniël van den Berg
 * @date 9/29/2015.
 * <p/>
 * The Contact class is a simple bridge that allows for users to add or view a contact based on entered details.
 */
public class Contact {
    public final String name;
    public final String email;
    public final String mobile;
    public final String mobile2;
    public final String phone;
    public final String phone2;

    /**
     * Creates a new contact. This contact can then be used to display or be added to the contact list.
     * @param name The name of the contact.
     * @param email The email of the contact.
     * @param mobile The first mobile number of the contact.
     * @param mobile2 The second mobile number of the contact.
     * @param phone The first phone number of the contact.
     * @param phone2 The second phone number of the contact.
     */
    public Contact(String name,
                   String email, String mobile, String mobile2, String phone, String phone2) {
        this.name = name;
        this.email = email;
        this.mobile = mobile;
        this.mobile2 = mobile2;
        this.phone = phone;
        this.phone2 = phone2;
    }

    /**
     * Returns a human-readable representation of this object. 
     * It will filter out any phone numbers that have been set to "" or null.
     * This will return the following string:<br>
     * <code>
     * name<br>
     * email<br>
     * mobile<br>
     * mobile2<br>
     * phone<br>
     * phone2
     * </code>
     * The line-breaks are implemented as "\n"
     */
    @Override
    public String toString() {
        String contactString = name + "\n";
        contactString += email + "\n";
        if (!mobile.isEmpty() &&
            !mobile.equalsIgnoreCase("null")) {
            contactString += mobile + "\n";
        }
        if (!mobile2.isEmpty() &&
            !mobile2.equalsIgnoreCase("null")) {
            contactString += mobile2 + "\n";
        }
        if (!phone.isEmpty() &&
            !phone
                    .equalsIgnoreCase("null")) {
            contactString += phone + "\n";
        }
        if (!phone2.isEmpty() &&
            !phone2
                    .equalsIgnoreCase("null")) {
            contactString += phone2;
        }
        return contactString;
    }

    /**
     * showOrCreateContact will show or create the contact. This function will search the phone's address book for any contact with the same e-mail address. 
     * If any is found, it will open that contact using the default contact app. If not, it will open an "add contact" dialog.
     * @param context The context to use when firing intents.
     */
    public void showOrCreateContact(Context context) {
        final Cursor
                cursor
                = context
                .getContentResolver()
                .query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                        new String[]{
                                ContactsContract.PhoneLookup._ID,
                                ContactsContract.PhoneLookup.LOOKUP_KEY},
                        ContactsContract.CommonDataKinds.Email.DATA +
                        " LIKE '" +
                        email +
                        "'",
                        null,
                        null);
        Log.d(
                "Contact",
                "Count " + cursor.getCount());
        if (cursor.moveToFirst()) {
            final String id = cursor.getString(0);
            Log.d("Contact", "ID " + id);
            Intent
                    intent
                    = new Intent(Intent.ACTION_VIEW);
            String
                    lookupKey
                    = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.LOOKUP_KEY));
            long contactId = cursor.getLong(
                    cursor.getColumnIndexOrThrow(
                            ContactsContract.PhoneLookup._ID));
            Uri uri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
            intent.setData(uri);

            context.startActivity(intent);
            return;
        }

        Intent
                intent
                = new Intent(Intent.ACTION_INSERT_OR_EDIT);
        intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
        intent.putExtra(
                ContactsContract.Intents.Insert.NAME,
                name);
        intent.putExtra(
                ContactsContract.Intents.Insert.EMAIL,
                email);
        ArrayList<ContentValues> data = new ArrayList<>();


        if (!mobile.isEmpty() &&
            !mobile
                    .equalsIgnoreCase("null")) {
            data.add(
                    addPhoneNumber(
                            mobile,
                            "Mobiel"));
        }
        if (!mobile2.isEmpty() &&
            !mobile2
                    .equalsIgnoreCase("null")) {
            data.add(
                    addPhoneNumber(
                            mobile2,
                            "Mobiel 2"));
        }

        if (!phone.isEmpty() &&
            !phone
                    .equalsIgnoreCase("null")) {
            data.add(
                    addPhoneNumber(
                            phone,
                            "Telefoon"));
        }
        if (!phone2.isEmpty() &&
            !phone2
                    .equalsIgnoreCase("null")) {
            data.add(
                    addPhoneNumber(
                            phone2,
                            "Telefoon 2"));
        }
        intent.putParcelableArrayListExtra(
                ContactsContract.Intents.Insert.DATA,
                data);
        intent.putExtra(
                "finishActivityOnSaveCompleted",
                true);
        context.startActivity(intent);
    }

    private ContentValues addPhoneNumber(String number, String text) {

        ContentValues row = new ContentValues();
        row.put(
                ContactsContract.Data.MIMETYPE,
                ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
        row.put(
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                number);
        row.put(
                ContactsContract.CommonDataKinds.Phone.TYPE,
                ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM);
        row.put(
                ContactsContract.CommonDataKinds.Phone.LABEL,
                text);
        return row;
    }
}