2017-09-17 70 views
0

我試圖導入聯繫人到我的應用程序,但努力獲取公司名稱。這裏是我的代碼:如何獲得聯繫人的公司名稱在Android

public List<ContactItem> getContactList(){ 
    ArrayList<ContactItem> contactList = new ArrayList<ContactItem>(); 

    Uri contactUri = ContactsContract.Contacts.CONTENT_URI; 
    String[] PROJECTION = new String[] { 
      ContactsContract.Contacts._ID, 
      ContactsContract.Contacts.DISPLAY_NAME, 
      ContactsContract.Contacts.HAS_PHONE_NUMBER, 
    }; 
    String SELECTION = ContactsContract.Contacts.HAS_PHONE_NUMBER + "='1'"; 
    Cursor contacts = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, SELECTION, null, null); 


    if (contacts.getCount() > 0) 
    { 
     while(contacts.moveToNext()) { 
      ContactItem aContact = new ContactItem(); 
      int idFieldColumnIndex = 0; 
      int nameFieldColumnIndex = 0; 
      int numberFieldColumnIndex = 0; 
      int companyFieldColumnIndex = 0; 

      String contactId = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts._ID)); 

      nameFieldColumnIndex = contacts.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME); 

      if (nameFieldColumnIndex > -1) 
      { 
       aContact.setName(contacts.getString(nameFieldColumnIndex)); 
      } 

      // Tried to get a company, but 
      // this always returns -1 
      companyFieldColumnIndex = contacts.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY); 
      if (companyFieldColumnIndex > -1) 
      { 
       Log.d(TAG, "getContactList: starts"); 
       aContact.setCompany(contacts.getString(companyFieldColumnIndex)); 
      } 


      PROJECTION = new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER}; 
      final Cursor phone = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, ContactsContract.Data.CONTACT_ID + "=?", new String[]{String.valueOf(contactId)}, null); 
      if(phone.moveToFirst()) { 
       while(!phone.isAfterLast()) 
       { 
        numberFieldColumnIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 

        if (numberFieldColumnIndex > -1) 
        { 
         aContact.setPhoneNum(phone.getString(numberFieldColumnIndex)); 
         phone.moveToNext(); 
         TelephonyManager mTelephonyMgr; 
         mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
         if (!mTelephonyMgr.getLine1Number().contains(aContact.getPhoneNum())) 
         { 
          contactList.add(aContact); 
         } 
        } 
       } 
      } 
      phone.close(); 
     } 

     contacts.close(); 
    } 

    return contactList; 
} 

我在我的代碼,在那裏我試圖讓聯繫人的公司名稱添加評論,但我總是得到-1。有些聯繫人具有公司名稱,因此在這部分代碼中出現錯誤。如何正確地獲得公司?

回答

1

首先,您正在傳遞Projection,其中不包含與公司名稱相關的任何內容,因此您當然不會得到它。

其他我不確定的事情是,您需要通過聯繫人的RawId而不是ID來獲取公司名稱。這裏的東西,你應該怎麼做,

String contactId = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts._ID)); 
String rawContactId = getRawContactId(contactId); 
String companyName = getCompanyName(rawContactId); 

&這裏的功能,你需要:

private String getRawContactId(String contactId) { 
     String[] projection = new String[]{ContactsContract.RawContacts._ID}; 
     String selection = ContactsContract.RawContacts.CONTACT_ID + "=?"; 
     String[] selectionArgs = new String[]{contactId}; 
     Cursor c = mContentResolver.query(ContactsContract.RawContacts.CONTENT_URI, projection, selection, selectionArgs, null); 
     if (c == null) return null; 
     int rawContactId = -1; 
     if (c.moveToFirst()) { 
      rawContactId = c.getInt(c.getColumnIndex(ContactsContract.RawContacts._ID)); 
     } 
     c.close(); 
     return String.valueOf(rawContactId); 

    } 

和:

private String getCompanyName(String rawContactId) { 
     try { 
      String orgWhere = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
      String[] orgWhereParams = new String[]{rawContactId, 
        ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}; 
      Cursor cursor = mContentResolver.query(ContactsContract.Data.CONTENT_URI, 
        null, orgWhere, orgWhereParams, null); 

      if (cursor == null) return null; 
      String name = null; 
      if (cursor.moveToFirst()) { 
       name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY)); 
      } 
      cursor.close(); 
      return name; 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return null; 
     } 

    } 
相關問題