tags:

views:

41

answers:

1

I have this code that has one button that let's me choose an entry from contacts, and passes that choesn contact to onActivityResult function. My question is how do I select data of that single contact when all that is passed is an Intent in data variable. That data variable, if converted to string shows something like "dat: content://contacts/people/4" so I see that selected contact is somehow passed, but what now? How to get that data?

And also all I found by googling was examples with deprecated class People, so I don't know how too use new classes.

Please help.

Thank you.

public class HelloAndroid extends Activity {

    private static final int CONTACT_ACTIVITY = 100; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button contactButton = (Button) findViewById(R.id.pick_contact_button);
        contactButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Uri uri = Uri.parse("content://contacts/people");
                Intent contacts_intent = new Intent(Intent.ACTION_PICK, uri);
                startActivityForResult(contacts_intent, CONTACT_ACTIVITY); 
            }
        });     

    }

     public void onActivityResult(int requestCode, int resultCode, Intent data){
          super.onActivityResult(requestCode, resultCode, data);

          switch(requestCode){
              case(CONTACT_ACTIVITY): {
                  if(resultCode == Activity.RESULT_OK) {
                      alertText(data.toString());
                  }
                 break;
              }
          }
    }
}
+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 = (Uri)data.toString();
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
i get an error on the first line Uri uri = (Uri)data.toString(); cannot cast from string to uri
dfilkovi
And also this line: String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));Craches my application, and I don't know why
dfilkovi
You said that your data variable holds a content URI. Check how can you convert to an URI. I don't know why that crashes your application.
Pentium10
Nope, I said that data variable holds Intent, but if converted to string shows some uri data, so there must be a way to get uro from intent.
dfilkovi
I found a method that gets URI from intent - getData(), now I only need to work out that crash... Thanx you helped a lot
dfilkovi