2

I followed this tutorial and got the basics of Content Providers : http://www.vogella.de/articles/AndroidSQLite/article.html

But wanted to know how can i get the contact number that is stored against display name. tried with "ContactsContract.Contacts.CONTENT_VCARD_TYPE". But got an error.

Please let me know if there is any solution.

Thanks
Sneha

Smitha
  • 6,110
  • 24
  • 90
  • 161
  • 1
    what is the error you are getting.? – 5hssba Mar 19 '12 at 07:16
  • here: http://stackoverflow.com/questions/9496350/pick-a-number-and-name-from-contacts-list-in-android-app/9496536#9496536 – drulabs Mar 19 '12 at 07:28
  • Thanks KKD.. but could u please explain me this: Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null); and how do we know that column name is "data1" in " cNumber = phones.getString(phones.getColumnIndex("data1")); " – Smitha Mar 19 '12 at 09:06

2 Answers2

4

This is a good tutorial about Getting contact number using content provider in android

http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/

and

http://www.app-solut.com/blog/2012/06/retrieval-of-contacts-with-contact-contract/

and can pick contact number like this

add button click event like

button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                i = new Intent(Intent.ACTION_PICK,
                        ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(i, PICK_CONTACT);
            }
        });


//outside button click

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

        switch (reqCode) {
        case (PICK_CONTACT):


            if (resultCode == Activity.RESULT_OK) {
                getContactInfo(data);
            }
        }
    }

    // onActivityResult

    private void getContactInfo(Intent data) {
        // TODO Auto-generated method stub

         ContentResolver cr = getContentResolver();




        Cursor cursor = managedQuery(data.getData(), null, null, null, null);
        while (cursor.moveToNext()) {
            String contactId = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts._ID));
            Name = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

            String hasPhone = cursor
                    .getString(cursor
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

            Cursor emailCur = cr.query( 
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                    new String[]{contactId}, null); 


            emailCur.close();






     if (hasPhone.equalsIgnoreCase("1"))
                hasPhone = "true";
            else
                hasPhone = "false";

            if (Boolean.parseBoolean(hasPhone)) {
                Cursor phones = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = " + contactId, null, null);
                while (phones.moveToNext()) {
                    phoneNo = phones
                            .getString(phones
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
                phones.close();
            }

            pname.setText(Name);
            //
            phno.setText(phoneNo);

        Toast.makeText(this, Name + "   " + phoneNo, Toast.LENGTH_SHORT).show();


        }

    }
Avi Kumar
  • 4,403
  • 8
  • 36
  • 67
2

This code will work for mobile number contacts, not for email or something. i found this code simplest. let me know if there is any problem.

start activity with pick intent on phone data type:

findViewById(R.id.choose_contact_button).setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View v) {
                Intent pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                startActivityForResult(pickContact, PICK_CONTACT_REQUEST);
          }
     });

Now set onAcitivityResult();

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent){
        switch (requestCode){
            case PICK_CONTACT_REQUEST:
                if (resultCode == RESULT_OK){
                    Uri contactUri = intent.getData();
                    Cursor nameCursor = getContentResolver().query(contactUri, null, null, null, null);
                    if (nameCursor.moveToFirst()){
                        String name = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String number = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        ((EditText)findViewById(R.id.person_name)).setText(name);
                        ((EditText)findViewById(R.id.enter_mobile)).setText(number);
                        nameCursor.close();
                    }
                }
                break;
        }
    }
Nishant Bhakta
  • 2,897
  • 3
  • 21
  • 24