sampathpremarathna
sampathpremarathna

Reputation: 4064

Retrieve HOME Contact Number from address book

currently i have a application which will retrieve all the contact details and will display all the available Contact Names.But now i want to retrieve Home,work numbers.I search for this everywhere but i couldn't. how can i achieve this? please help me.

Thanks.

I used this code to get Home Numbers.

         Cursor c = getContentResolver().query(Data.CONTENT_URI,
                new String[] {Data._ID, Phone.NUMBER, Data.MIMETYPE},
                 Data.RAW_CONTACT_ID + "=?" + " AND "
                          + Data.MIMETYPE + "='" + Phone.TYPE_HOME + "'",
                 new String[] {String.valueOf(contactId)}, null);

But what i need is to get Home,Work,Mobile (with name,email address,etc) numbers using single query.

using this code it always returns me type=0

Cursor c = getContentResolver().query(Data.CONTENT_URI,
                  null,
                  Data.CONTACT_ID + "=?",
                  new String[] {String.valueOf(contactId)}, null);

           while(c.moveToNext()){

           int type = c.getInt(c.getColumnIndex(Phone.TYPE));
           ..
           }

Upvotes: 0

Views: 221

Answers (3)

ngesh
ngesh

Reputation: 13501

In where clause of your query use Phone.TYPE_HOME.. this will give you the desired nor..

Upvotes: 0

sampathpremarathna
sampathpremarathna

Reputation: 4064

Ok finally i completed the task.here my code

      String mobile = "";
  String home="";
  String work="";
  String fax="";
  String other="";

  String disName="";
  String pName="";
  String fName="";
  String lName="";
  String sName="";
  String mName="";

  String postBox="";
  String streat="";
  String country="";

  String emailAdd="";

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null);
       while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            disName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));




    ///*   
       Cursor phones = cr.query(Phone.CONTENT_URI, null,
               Phone.CONTACT_ID + " = " + id, null, null);
           while (phones.moveToNext()) {
               String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
               int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
               System.out.println("Numeber= "+number);
               switch (type) {
                   case Phone.TYPE_HOME:
                       home=number;
                       break;
                   case Phone.TYPE_MOBILE:
                       mobile=number;
                       break;
                   case Phone.TYPE_WORK:
                       work=number;
                       break;                       
                   case Phone.TYPE_OTHER:
                       other=number;
                       break;
                   case Phone.TYPE_FAX_HOME:
                       fax=number;
                       break;
                   case Phone.TYPE_FAX_WORK:
                       fax=number;
                       break;
           }              

           }
           phones.close();

           Cursor name = cr.query(Data.CONTENT_URI, null,
                   Data.CONTACT_ID + " ="+id +" AND "+Data.MIMETYPE+"='"+StructuredName.CONTENT_ITEM_TYPE+"'", null, null);   
           while(name.moveToNext()){
            sName=name.getString(name.getColumnIndex(StructuredName.PREFIX));
            fName=name.getString(name.getColumnIndex(StructuredName.GIVEN_NAME));
            mName=name.getString(name.getColumnIndex(StructuredName.MIDDLE_NAME));
            lName=name.getString(name.getColumnIndex(StructuredName.FAMILY_NAME));
            sName=name.getString(name.getColumnIndex(StructuredName.SUFFIX));
            System.out.println(mName);
           }


           Cursor address = cr.query(Data.CONTENT_URI, null,
                   Data.CONTACT_ID + " ="+id +" AND "+Data.MIMETYPE+"='"+StructuredPostal.CONTENT_ITEM_TYPE+"'", null, null);   
           while(address.moveToNext()){
            postBox=address.getString(address.getColumnIndex(StructuredPostal.POBOX));
            streat=address.getString(address.getColumnIndex(StructuredPostal.STREET));
            mName=address.getString(address.getColumnIndex(StructuredPostal.COUNTRY));

            System.out.println(postBox);
           }

           Cursor email = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                   Data.CONTACT_ID + " ="+id , null, null);   
           while(email.moveToNext()){
            emailAdd=email.getString(email.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
           }

Upvotes: 0

Vineet Shukla
Vineet Shukla

Reputation: 24031

try this:
  int type = mCursor.getInt(mCursor.getColumnIndex(Phone.TYPE));
       the "type" value will be 1,2,3 or 4 where
        TYPE_HOME = 1;
        TYPE_MOBILE = 2;
        TYPE_WORK = 3;
        TYPE_OTHER = 7;

Upvotes: 1

Related Questions