正确显示联系信息

时间:2015-09-24 12:56:34

标签: android android-contacts

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

目前我有这样的设置:

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);

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

我想要实现的是让android看到这些信息是链接的。只需显示电话号码,无论何时点击文本视图中的任何位置,都会弹出一个弹出窗口显示所有联系信息和一个选项"添加到联系人",最好是the default android contact popup

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

3 个答案:

答案 0 :(得分:1)

您可以使用Intents.Insert.ACTION,它将使用必需的数据调用Contacts应用程序。您需要做的就是添加所需的数据,如电话号码,电子邮件等,作为额外的意图。

// 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

答案 1 :(得分:0)

我建议你研究一下使用Gson类来有效地解析你的JSON对象。它会像,

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

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

答案 2 :(得分:0)

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

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

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;
    }
}
相关问题