0

Possible Duplicate:
get contact info from android contact picker

hello i m trying to get contact number from android contactlist in my app below is code in which i have given one button onclick of which i am invoking intent to puck a contact,i also have added read_contact permision in menifest file, but I am getting result code value -1 ....wat is wrong??? can anyone pls help me...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.friendinvite);

    Button btnFriend=(Button)findViewById(R.id.btnFriend);
    btnFriend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent contact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(contact, 1);        


        }
    });

}
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {


                ContentResolver cr = getContentResolver();

                Uri contactData = data.getData();

                Cursor c = managedQuery(contactData,null,null,null,null);
                String id=null;
                String name=null;
                String phone=null;

                if(c.moveToFirst()){
                        id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                        name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                            Cursor pCur = cr.query(Phone.CONTENT_URI,null,Phone.CONTACT_ID +" = ?", new String[]{id}, null);

                      while(pCur.moveToNext()){
                          phone = c.getString(c.getColumnIndex(Phone.NUMBER));

                      }
                  }


                }
        }
    }
Community
  • 1
  • 1

1 Answers1

1

Have you looked at Contacts Provider Api? Seems they don't do it the way you do. I would suggest starting with the Uri information.

EDIT: Seems that the activity uses two ways to call startactivityforresult(). Maybe after reading the Activity guide you'll have better insight.

Eric Fossum
  • 2,395
  • 4
  • 26
  • 50
  • i searched many tutorials n dey following d same way dat i m following.. i dont think der would b any problem.. – user1879852 Dec 11 '12 at 17:39
  • 1
    Well hopefully your code is better than your English otherwise I don't think you're following it to the letter. – Eric Fossum Dec 11 '12 at 17:43