【问题标题】:Properly display contact information正确显示联系信息
【发布时间】:2015-09-24 12:56:33
【问题描述】:

我正在制作一个 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 看到这个信息是链接的。这不仅仅是显示电话号码,每当您点击文本视图中的任何位置时,都会弹出一个显示所有联系信息和“添加到联系人”选项的弹出窗口,最好像

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

【问题讨论】:

  • 您想显示自定义对话框以获取联系信息吗?
  • 通过不同的选项,如果您指的是“共享方式”选项,那么可以通过使用 ACTION_SHARE 触发意图来完成,并且所有接受这种意图的应用程序都会显示出来。
  • @praveenSharma 我不一定需要自定义对话框,启动默认对话框也可以。
  • @cafebabe1991 不,抱歉。我想这还不够清楚,不想分享联系信息。我有联系方式,就是想好好展示一下。
  • 看看你是否想在你的应用中显示联系信息然后你需要制作你的 UI。您共享的图片只是使用 Intent 的默认共享选项

标签: android android-contacts


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我建议您考虑使用 Gson 类来有​​效地解析您的 JSON 对象。它会像,

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

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

    【讨论】:

      【解决方案3】:

      感谢 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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-18
        相关资源
        最近更新 更多