views:

100

answers:

1

Hi,

I tried so much tutorials, and read a lot here at SO, but i cant solve my problem:

When a button is clicked, the user can choose the mobile number of a contact. Actual I can get the name of the selected contact, but i can't find a way, to get/chose the mobile number..

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /** Layouting */
    this.mGetMobileNumberButton = (Button)findViewById(R.id.getMobileNumberButton);
    this.mNameTextView = (TextView)findViewById(R.id.nameTextView);
    this.mMobileNumberTextView = (TextView)findViewById(R.id.mobileNumberTextView);


    /** onClick getContactInfos*/
   this.mGetMobileNumberButton.setOnClickListener(new OnClickListener() {
      public void onClick(View v){ 
          Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
          startActivityForResult(intent, 1);        
      } 
    });
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        Uri contactData = data.getData();
        Cursor c = managedQuery(contactData, null, null, null, null);
        if (c.moveToFirst()) {
            String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
            mNameTextView.setText(name);
        }
    }
}

Hope anyone can help :)

+1  A: 

This will get the cursor holding base contact data, and will loop through the phone numbers the contact has, can have multiple.

    Uri uri = data.getData();
Cursor cursor=ctx.getContentResolver().query(uri, null, null, null, null);

   while (cursor.moveToNext()) { 
   String contactId = cursor.getString(cursor.getColumnIndex( 
     ContactsContract.Contacts._ID)); 
   String hasPhone = cursor.getString(cursor.getColumnIndex( 
     ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
   if (Boolean.parseBoolean(hasPhone)) { 
                // You know have the number so now query it like this
Cursor phones = getContentResolver().query( 
  ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
  null, 
  ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, 
      null, null); 
    while (phones.moveToNext()) { 
     String phoneNumber = phones.getString( 
       phones.getColumnIndex( 
         ContactsContract.CommonDataKinds.Phone.NUMBER));                 
    } 
    phones.close(); 
   } 
 }
Pentium10
thanks for ur answer, but i dont know what to do with "ctx" in this line:
Christoph
ursor cursor=ctx.getContentResolver().query(uri, null, null, null, null);
Christoph
ctx is the context
Pentium10
it is still not working, i replaced ctx with this. When i say mAnything.setText(contactId) it works. But when i try mAnything.setText(phoneNumber) it doesnt work :(
Christoph
You are doing something wrong, raise a Toast with the results to see if at all they are coming up. The above code works just fine. See a working tutorial here: http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/
Pentium10
ok, i think i got it, "if(Boolean.parseBoolean(hasPhone))" doesnt work in my code, i replaced it with if "(Integer.parseInt(cursor.getString( cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)"
Christoph