Question

I am trying to pick a contact from Default Contact app. I got information from a contact successfuly. Now i want to save this data in an arraylist, type of contact. how from onActivityResult? I am searching this for 5 hours.I am new in android.

here is my onActivityResult's code

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

    switch (reqCode) {
    case (PICK_CONTACT):
        if (resultCode == RESULT_OK) {
            Uri contactData = data.getData();
            ContentResolver cr = getContentResolver();
            Cursor c = cr.query(contactData, null, null, null, null);
            if (c.moveToFirst()) {
                String id = c
                        .getString(c
                                .getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                long cId = (long)Double.parseDouble(id);

                String name = c
                        .getString(c
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                int hasPhoneNumber = c
                        .getInt(c
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                if (hasPhoneNumber > 0) {
                    // this contact
                    Cursor phoneCursor = cr
                            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                    null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                            + "=?", new String[] { id },
                                    null);
                    while (phoneCursor.moveToNext()) {
                        String phoneNumber = phoneCursor
                                .getString(phoneCursor
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        Toast.makeText(getApplicationContext(),
                                id + " " + name + " " + phoneNumber,
                                Toast.LENGTH_LONG).show();
                    }
                    phoneCursor.close();
                }
            }
        }
    }

}
Was it helpful?

Solution

In the above after assigning value to the

String phoneNumber = phoneCursor .getString(phoneCursor .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

store the same value in the INTENT object i.e

Intent data

like data.putExtra("phoneNumber",phoneNumber);

and after the start the new activity which you want to invoke like below

startActivity(new Intent(Intent.ACTION_VIEW, data));

in the new activity get the above value from intent

Intent intentWithPhoneNumbers = getIntent(); if(null!=intentWithPhoneNumbers.getExtras()){ String phoneNumber= intentWithPhoneNumbers.getExtras().getString("phoneNumber"); }

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top