【问题标题】:Not able to read contact's all values无法读取联系人的所有值
【发布时间】:2009-10-13 08:09:22
【问题描述】:

我已经申请了从 android 通讯录中读取所有联系人及其值的应用程序,但我无法读取所有联系人的值。 目前我只得到它的名字和手机号码。

我怎样才能得到它的所有电话号码、所有 eamil 值和地址值?

提前谢谢...

【问题讨论】:

  • 我正在尝试从我的应用程序中读取联系人详细信息。我有光标对象。我怎样才能找到联系人的所有详细信息。我想同步我的联系人。
  • Uri 联系人 = Uri.parse("content://contacts/people");光标 cur = managedQuery(contacts, projection, null, null, null); if (cur.moveToFirst()) { 字符串名称;字符串电话号码; int nameColumn = cur.getColumnIndex(People.NAME); int phoneColumn = cur.getColumnIndex(People.NUMBER);做 { name = cur.getString(nameColumn); phoneNumber = cur.getString(phoneColumn); } 而 (cur.moveToNext()); }
  • 究竟是什么不工作?此外,您没有提取电子邮件列,这可以解释为什么您没有得到该结果。
  • 我怎样才能找到 eamil 和地址列。我没有得到任何返回 eamil 和地址列的东西,例如 phoneColumn = cur.getColumnIndex(People.NUMBER);用于电话列。

标签: java android


【解决方案1】:

首先,不要将代码放在 cmets 中。编辑您的原始帖子,这样更易​​于阅读。

其次,这对 Android 来说有点复杂。联系信息并非都在同一个地方,因此如果您想要全部,您将不得不执行多个查询。下面是从this thread 剪切和粘贴所有电话号码的示例:

void trythiscode(){
   // An array specifying which columns to return.
   String[] projection = new String[] {
   People._ID,
   People.NAME,
   People.NUMBER,
};

   // Get the base URI for People table in Contacts content provider.
   // which is: content://contacts/people/
   Uri contactUri = People.CONTENT_URI;

   // Best way to retrieve a query; returns a managed query.
   Cursor peopleCursor = managedQuery (contactUri,
     projection, //Which columns to return.
     null, // WHERE clause--we won't specify.
     null, // Selection Args??
     People.DEFAULT_SORT_ORDER); // Order-by name

   // go to the beginning of the list
   peopleCursor.moveToFirst();


   // So, here we have a contact. We need to get the contact ID (_id) then
   // build the Uri to get the phones section of that user's record
   // which is a subdirectory of a contact record

   long personId = peopleCursor.getLong(peopleCursor.getColumnIndex("_id"));

   Uri personUri = ContentUris.withAppendedId(contactUri, personId );

   // So now the URL looks like: content://contacts/people/_id(where the actual id of the record is here)
   Uri phoneUri=
    Uri.withAppendedPath(personUri, Contacts.People.Phones.CONTENT_DIRECTORY);

   // Now the URL looks like: content://contacts/people/_id/phones (where phones is literally "phones")

   // Now get all the phone numbers for this contact
   Cursor phonesCursor = managedQuery(phoneUri,
     null,
     null,
     null,
     Phones.DEFAULT_SORT_ORDER);

   // We now have a cursor for all the phone numbers for that User ID
   // go to the beginning of the phone list.
   phonesCursor.moveToFirst();


}

而且这完全有可能已经过时(我没有尝试过)。您将不得不花一些时间阅读文档,以防他们更新了 Doughnut 中的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-14
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多