2017-10-13 75 views
0

我正在創建一個應該列出聯繫人和聯繫人電話的應用程序。我可以做的聯繫人列表,但我無法列出與特定聯繫人關聯的電話號碼。 我一直在網絡上找到一個可行的解決方案來解決我的問題,但我無法找到解決方案。Xamarin表單聯繫人和電話號碼

這是讀取和列出聯繫人的代碼。

public List<Contato> ListaContato() 
    { 
     var uri = ContactsContract.Contacts.ContentUri; 

     string[] projection = { ContactsContract.Contacts.InterfaceConsts.Id, ContactsContract.Contacts.InterfaceConsts.DisplayName, ContactsContract.Contacts.InterfaceConsts.PhotoId }; 

     var cursor = Context.ContentResolver.Query(uri, projection, null, null, null); 

     var contactList = new List<Contato>(); 

     if (cursor.MoveToFirst()) 
     { 
      do 
      { 
       Contato contato = new Contato(); 
       Telefone telefone = new Telefone(); 

       contato.Id = cursor.GetInt(cursor.GetColumnIndex(projection[0])); 
       contato.NomeContato = cursor.GetString(cursor.GetColumnIndex(projection[1])); 
       contato.FotoContato = cursor.GetString(cursor.GetColumnIndex(projection[2])); 

       contato.Telefone = CarregarTelefone(contato.Id.ToString()); 

       contactList.Add(contato); 
      } 
      while (cursor.MoveToNext()); 
     } 

     return contactList; 
    } 

此另一種方法是使與聯繫人相關聯的電話號碼列表。

public List<Telefone> CarregarTelefone(string contactId) 
    { 
     //ContactsContract.CommonDataKinds.Phone.SearchDisplayNameKey 
     var cursor = Context.ContentResolver.Query(Phone.ContentUri, new String[] { Phone.Number }, ContactsContract.CommonDataKinds.Identity.IdentityColumnId + "=" + contactId, null, null); 
     //var c = Context.ContentResolver.Query(Phone.ContentUri, new String[] { Phone.Number }, Phone.ContentItemType + "=" + Phone.TYPE_MOBILE + " and " + Phone.CONTACT_ID + "=" + contactId, null, null); 


     List<Telefone> litel = new List<Telefone>(); 

     //if (cursor.MoveToNext()) 
     //{ 
     // Telefone tele = new Telefone(); 
     // tele.NumeroTelefone = cursor.GetString(0); 
     // litel.Add(tele); 
     //} 

     while (cursor.MoveToNext()) 
     { 
      Telefone tele = new Telefone(); 
      tele.NumeroTelefone = cursor.GetString(0); 
      litel.Add(tele); 
     } 

     return litel; 
    } 

我已經使用此代碼作爲參考,但它並沒有完全服務於我。

光標= contentResolver.query(Phone.CONTENT_URI, 新的String [] {Phone.NUMBER}, Phone.TYPE + 「=」 + Phone.TYPE_MOBILE + 「和」 + + Phone.CONTACT_ID 「=」 + contactId,null,null);

的代碼包含在此鏈接:ContentResolver.Query()

如何,那麼,可以列出在我xamarin應用程序的聯繫人的所有電話號碼?


晚安人! 關於發現手機標籤(手機,家庭,工作等)的問題,我想說,我已經找到了解決方案,我在這裏與你分享。

public List<Telefone> CarregarTelefone(string contactId) 
    { 
     var uri = ContactsContract.Contacts.ContentUri; 

     string[] projection = { Phone.Number, CommonColumns.Type }; 

     var cursor = Context.ContentResolver.Query(Phone.ContentUri, projection, ContactsContract.RawContactsColumns.ContactId + "=" + contactId, null, null); 

     List<Telefone> litel = new List<Telefone>(); 

     if (cursor != null) 
     { 
      while (cursor.MoveToNext()) 
      { 
       Telefone tele = new Telefone(); 
       tele.NumeroTelefone = cursor.GetString(0); 
       tele.Etiqueta = CarregarEtiqueta(int.Parse(cursor.GetString(1))); 
       litel.Add(tele); 
      } 
     } 

     cursor.Close(); 

     return litel; 
    } 

現在我仍然需要幫助解決列出聯繫人組(家庭,工作,同學,教師等)的問題。我在網上看過一些東西,但我還沒有成功。任何提示都非常受歡迎。

回答

0

我正在創建一個應該列出聯繫人和聯繫人電話的應用程序。我可以做的聯繫人列表,但我無法列出與特定聯繫人關聯的電話號碼。我一直在網絡上尋找可行的解決方案,但我無法找到解決方案。

根據contactId,因爲你使用了錯誤的過濾字符串不能檢索的電話號碼:

ContactsContract.CommonDataKinds.Identity.IdentityColumnId + "=" + contactId

正確的過濾器看起來是這樣的:

ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId + "=" + contactId

所以CarregarTelefone應該看起來像這樣:

public List<Telefone> CarregarTelefone(string contactId) 
{ 
    var cursor = this.ContentResolver.Query(ContactsContract.CommonDataKinds.Phone.ContentUri, new string[] { Phone.Number }, ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId + "=" + contactId, null, null); 

    List<Telefone> litel = new List<Telefone>(); 

    while (cursor.MoveToNext()) 
    { 
     Telefone tele = new Telefone(); 
     tele.NumeroTelefone = cursor.GetString(0); 
     litel.Add(tele); 
    } 

    return litel; 
} 

更新:

對於搜索的禮儀,你可以在ContactsContract.CommonDataKinds.Phone.InterfaceConsts.Type列添加到搜索欄:

var cursor = this.ContentResolver.Query(ContactsContract.CommonDataKinds.Phone.ContentUri, new string[] { Phone.Number, Phone.InterfaceConsts.Type }, ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId + "=" + contactId, null, null); 

它會給你一個int value.You可以比較INT與type table獲得當前類型的手機。

+0

謝謝! 這真的解決了我的問題。 現在,不想濫用您的善意,您是否知道如何列出特定聯繫人所屬的組? –

+0

另一件事:如何找出電話號碼(手機,固定電話,工作)的禮節? –

+0

通過禮儀,你的意思是像「移動\家\工作\工作傳真」? –

相關問題