2014-09-22 72 views
0

从我的联系人列表中选择一个联系人后,它不会只获得一个联系人,而是只会获得一个联系人。从Android应用程序中的联系人列表中获取一个联系人

下面是我在做什么,以获取电话号码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     if (resultCode == RESULT_OK) { 
      switch (requestCode) { 
      case CONTACT_PICKER_RESULT: 
       ContentResolver cr = getContentResolver(); 
       Cursor c = cr.query(ContactsContract.Contacts.CONTENT_URI, 
         null, null, null, null); 
       if (c.moveToFirst()) { 
        String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID)); 
        Cursor phone = cr.query(Phone.CONTENT_URI,null,null,null,null); 
        while(phone.moveToNext()){ 
         String number = phone.getString(phone.getColumnIndex(Phone.NUMBER)); 
         int type = phone.getInt(phone.getColumnIndex(Phone.TYPE)); 
         this.numeros.add(number); 
        }     
       } 
       break; 
      } 
      this.mostarToast(this.numeros.size() + " contatos adicionados."); 
     } else { 
      // gracefully handle failure 
      Log.w("Erro", "Erro ao adicionar contato!"); 
     } 
    } 

的“mostrarToast”方法只显示有多少接触我选择。但不是一个,它显示了101个联系人。我怎样才能选择一个?

回答

0

我认为那youre采摘,而不是单一的一个所有数字:

Cursor phone = cr.query(Phone.CONTENT_URI,null,ContactsContract.Contacts._ID+" = ?" ,new String[]{id},null); 

这将只接触式ID获取所有的数字,试试吧

+0

嗯......我试过,但它给了我0 results .... – 2014-09-22 22:25:28

+0

有些联系人可以没有电话号码,你可以尝试与另一个。 – Eefret 2014-09-23 03:57:28

相关问题