1

The details of the contacts not displayed properly by using this code. I am trying to access the Name, Number and Email ID by using the Intent. Name and Number only displayed when I click the button but not the Email.No error in the project.It's working good.My xml file have only one Button.

public class GetDetails extends Activity {

/** Called when the activity is first created. */
private static final int CONTACT_PICKER_RESULT = 1001; 
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

       Button Btn = (Button)findViewById(R.id.getContacts);
        Btn.setOnClickListener(new View.OnClickListener() { 

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

            } 
        });
    }

    protected void onActivityResult(int reqCode, int resultCode, Intent data) { 
        super.onActivityResult(reqCode, resultCode, data);
        if(resultCode == RESULT_OK) {
            switch (reqCode) {
            case CONTACT_PICKER_RESULT:
                Cursor cursor = null;
                Cursor emails = null;
                String number = "";
                String emailID = "";
                try {

                    Uri result = data.getData();

                    //get the id from the uri
                    String id = result.getLastPathSegment();  

                    //query
                    cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone._ID + " = ? " , new String[] {id}, null);

                    int numberIdx = cursor.getColumnIndex(Phone.DATA);  
                     if(cursor.moveToFirst()) {
                        number = cursor.getString(numberIdx);

                    } else {

                    }
                      emails = getContentResolver().query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
                     int num = emails.getColumnIndex(Email.DATA);
                     if(emails.moveToFirst()) {
                         emailID = emails.getString(num);

                     } else { 

                     }

                } catch (Exception e) {
                    //failed
                } finally {
                    if (cursor!=null) {
                        cursor.close();
                    }


                }

            }
        }
    }
}

How to solve this situation ?

BigBoss
  • 85
  • 1
  • 4
  • 13
  • http://stackoverflow.com/questions/11032030/how-to-get-contact-information-in-android I answered this here – Aashish Bhatnagar Jun 14 '12 at 11:28
  • will your answer retrieve the name number and email Id ? – BigBoss Jun 14 '12 at 11:34
  • But it shows some error like " it cannot be resolved" which of them are variables. errors on adapter, setReminder, mcontext, numbers, chooseContactArray ... I created some variables apart from that it will increase the errors. – BigBoss Jun 14 '12 at 12:02

1 Answers1

0

Replace this line:

emails = getContentResolver().query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);

with this line :

emails = getContentResolver().query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = ?", new String[]{id}, null);
Arun George
  • 18,352
  • 4
  • 28
  • 28
  • Arun, No change is any other way ? – BigBoss Jun 14 '12 at 11:42
  • Are you able to see the name and number? – Arun George Jun 14 '12 at 11:48
  • Sure I will get the name and number ,after I try to get the Email also at that time nothing would be displayed. I think little error on the query. – BigBoss Jun 14 '12 at 11:51
  • A small query... Are you sure that the contact you are trying to access does have an email address registered in your device? – Arun George Jun 14 '12 at 11:54
  • Actually I have 7 contacts in Emulator device, all have number and email id except 1 contact. name is compulsory for all 7. for that 1 contact I saved only name and email ID ,but it would'nt displayed in the screen.all others are on the display – BigBoss Jun 14 '12 at 12:09
  • I have used your code.Code has no red marks and errors but nothing would be changed.the same name and number would display.no email id displayed on screen – BigBoss Jun 14 '12 at 12:54