2016-10-29 4 views
0

私は、以下のコードの連絡先の名前を取得することがわかります。しかし、このコードで連絡先リストに関連付けられている電話番号も変更するにはどうすればよいですか?Xamarin android連絡先と番号を読み取る

var uri = ContactsContract.Contacts.ContentUri; 

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

      var cursor = ManagedQuery(uri, projection, null, null, null); 

      var contactList = new List<string>(); 

      if (cursor.MoveToFirst()) 
      { 
       do 
       { 
        String phoneNumber = cursor.GetString(cursor.GetColumnIndex(ContactsContract.CommonDataKinds.Phone.Number)); 

       contactList.Add(cursor.GetString(
         cursor.GetColumnIndex(projection[1]))); 
      } while (cursor.MoveToNext()); 
     }     

答えて

0

私は最近同様のコードを書いている間にあなたの質問に出会った。ウリ ContactsContract.CommonDataKinds.Phone.ContentUriに別のクエリを作成し

  1. ;:あなたは電話番号を取得するために必要な唯一のものは、にあります
  2. ContactsContract.CommonDataKinds.Phone.Numberを含むように投影を設定します。このクエリは、のために、あなたの電話番号をもたらすでしょう

    string[] projection = { ontactsContract.CommonDataKinds.Phone.Number }; 
    string selection = "_id = " + contactId; 
    
    var cursor = ContentResolver.Query(ContactsContract.CommonDataKinds.Phone.ContentUri, projection, selection, null, null); 
    

    :と

  3. がIDに基づいて選択し、すなわち選択パラメータは「_id =」+ contactId

あなたのクエリは次のようになります含める必要があります連絡先IDは、以下のコードでは、パフォーマンス上の問題のため、私はすべての電話番号を1つの呼び出しを行い、各契約に番号を割り当てます。それが役に立てば幸い。

private List<Contact> GetContactList() 
{ 
    List<Contact> contacts = new List<Contact>(); 
    string[] projection = { 
       ContactsContract.Contacts.InterfaceConsts.Id, 
       ContactsContract.Contacts.InterfaceConsts.DisplayName, 
       ContactsContract.Contacts.InterfaceConsts.PhotoUri 
      }; 
    var uri = ContactsContract.Contacts.ContentUri; 
    ICursor cursor = ContentResolver.Query(uri, projection, null, null, null); 

    if (cursor.MoveToFirst()) 
    { 
     do 
     { 
      string id = cursor.GetString(cursor.GetColumnIndex(projection[0])); 
      string name = cursor.GetString(cursor.GetColumnIndex(projection[1])); 
      string photoUri = cursor.GetString(cursor.GetColumnIndex(projection[2])); 
      contacts.Add(new Contact() { Id = long.Parse(id), DisplayName = name, PhotoUri = photoUri }); 
     } while (cursor.MoveToNext()); 

     GetContactPhoneNumber(contacts); 
    } 

    return contacts; 
} 

private async void GetContactPhoneNumber(List<Contact> list) 
{ 
    string[] projection = 
     { 
        ContactsContract.CommonDataKinds.Phone.Number, 
        ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId 
       }; 

    var cursor = ContentResolver.Query(ContactsContract.CommonDataKinds.Phone.ContentUri, projection, null, null, null); 
    if (cursor.Count > 0) 
    { 
     await Task.Factory.StartNew(() => 
     { 
      do 
      { 
       try 
       { 
        string id = cursor.GetString(cursor.GetColumnIndex(projection[1])); 
        string phoneNumber = cursor.GetString(cursor.GetColumnIndex(projection[0])); 
        Contact contact = list.Where(c => c.Id == long.Parse(id)).FirstOrDefault(); 
        contact.PhoneNumber = phoneNumber; 
       } 
       catch 
       { 
       } 

      } while (cursor.MoveToNext()); 
      cursor.Close(); 
     }); 
    } 
} 
関連する問題