Thursday 8 August 2013

Get all contact details from contact id

//globly declaration above oncreate method

static ArrayList<Address> addresarylst; static ArrayList<Email> emailarylst;  
static ArrayList<Im> imarylst;
static ArrayList<Organization> orgarylst;
static ArrayList<Phone> phonearylst;


Method to get Name,Number and photoid from contact is

public String getNumber(String contid){
       Cursor cursor_phone = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + contid, null, null);
       while (cursor_phone.moveToNext()) {
         String  cont_phone = cursor_phone.getString(cursor_phone.getColumnIndex(Phone.NUMBER));
       } System.out.println("cont_phone"+cont_phone);
    return cont_phone;
   }
   
   public String getName(String contid){
       Cursor cursor_name = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + contid, null, null);
       while (cursor_name.moveToNext()) {
          String cont_name = cursor_name.getString(cursor_name.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
       } System.out.println("cont_name..."+cont_name);
    return cont_name;
   }
  
   public String getPhoto(String contid){
       Cursor cursor_photo = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + contid, null, null);
       while (cursor_photo.moveToNext()) {
         String  photoId = cursor_photo.getString(cursor_photo.getColumnIndex(Contacts.PHOTO_ID));
       } System.out.println("photoId..."+photoId);
    return photoId;
   }


Method to Get Email Id from contact id

private static ArrayList<Email> getEmails(String id,
            ContentResolver contentResolver) {
        Cursor emailCursor = contentResolver.query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + QUESTION,
                new String[] { id }, null);
        while (emailCursor.moveToNext()) {
            String value = emailCursor
                    .getString(emailCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            int type = emailCursor
                    .getInt(emailCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
            String rowId = emailCursor
                    .getString(emailCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Email._ID));
            if (ContactsContract.CommonDataKinds.Email.TYPE_HOME == type
                    || ContactsContract.CommonDataKinds.Email.TYPE_WORK == type
                    || ContactsContract.CommonDataKinds.Email.TYPE_OTHER == type) {


            
Email emailsetget = new Email();
                emailsetget.setValue(value);
                emailsetget.setRowId(rowId);
                emailsetget.setType(String.valueOf(type));
                emailarylst.add(emailsetget);
            }
        }
        contmainsetget.setEmails(emailarylst);
        emailCursor.close();
        return emailarylst;
    }



here Email is your getter setter class as i define below

public class Email {
    private String type = null;
    private String value = null;
    private String rowId = null;
    public Email() {
        super();
    }
   
    public Email(String type,String value) {
        super();
        //this.rowId = rowid;
        this.value = value;
        this.type = type;
    }
   
    public String getRowId() {
        return rowId;
    }
    public void setRowId(String rowId) {
        this.rowId = rowId;
    }
   
    public String getValue() {
        return ContactUtility.replaceNull(value);
    }
    public String getType() {
        return ContactUtility.replaceNull(type);
    }
    public void setType(String type) {
        this.type = type;
    }
    public void setValue(String value) {
        this.value = value;
    }

} 

Method to get address from contact id



    private static ArrayList<Address> getAddresses(String id,
            ContentResolver contentResolver) {
       
        Cursor addressCursor = contentResolver
                .query(ContactsContract.Data.CONTENT_URI,
                        null,
                        ContactsContract.Data.CONTACT_ID + QUESTION_AND
                                + ContactsContract.Data.MIMETYPE + QUESTION,
                        new String[] {
                                id,
                                ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE },
                        null);
        while (addressCursor.moveToNext()) {
            int type = addressCursor
                    .getInt(addressCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
            if (ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME == type
                    || ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK == type
                    || ContactsContract.CommonDataKinds.StructuredPostal.TYPE_OTHER == type) {
                String street = addressCursor
                        .getString(addressCursor
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                String city = addressCursor
                        .getString(addressCursor
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                String state = addressCursor
                        .getString(addressCursor
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                String poBox = addressCursor
                        .getString(addressCursor
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
                String zip = addressCursor
                        .getString(addressCursor
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                String country = addressCursor
                        .getString(addressCursor
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
                String rowId = addressCursor
                        .getString(addressCursor
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal._ID));

              
Address addsetget = new Address();
                addsetget.setCity(city);
                addsetget.setCountry(country);
                addsetget.setPoBox(poBox);
                addsetget.setState(state);
                addsetget.setType(String.valueOf(type));
                addsetget.setZip(zip);
                addsetget.setStreet(street);
                addsetget.setRowId(rowId);
                addresarylst.add(addsetget);
            }
        }
        addressCursor.close();
        return addresarylst;
    }


here Address is your getter setter class ,which i define below

public class Address {
    private String type = null;
    private String street = null;
    private String city = null;
    private String state = null;
    private String zip = null;
    private String country = null;
    private String rowId = null;
    private String poBox;
    private String neighborhood;

    public Address() {
        super();
    }

    public Address(String type,String street,String city,String state,String zip,String country,String pobox,String negieobrhood){
        super();
        //this.rowId = rowid;
        this.type = type;
        this.street = street;
        this.city = city;
        this.state = state;
        this.zip = zip;
        this.country = country;
        this.poBox = pobox;
        this.neighborhood = negieobrhood;
    }
   
    public String getRowId() {
        return rowId;
    }
    public void setRowId(String rowId) {
        this.rowId = rowId;
    }
    public String getType() {
        return ContactUtility.replaceNull(type);
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getStreet() {
        return ContactUtility.replaceNull(street);
    }
    public void setStreet(String street) {
        this.street = street;
    }
    public String getCity() {
        return ContactUtility.replaceNull(city);
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return ContactUtility.replaceNull(state);
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getZip() {
        return ContactUtility.replaceNull(zip);
    }
    public void setZip(String zip) {
        this.zip = zip;
    }
    public String getCountry() {
        return ContactUtility.replaceNull(country);
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getPoBox() {
        return ContactUtility.replaceNull(poBox);
    }
    public void setPoBox(String poBox) {
        this.poBox = poBox;
    }

    public String getNeighborhood() {
        return neighborhood;
    }

    public void setNeighborhood(String neighborhood) {
        this.neighborhood = neighborhood;
    }
}


Method to get IM details from contact is

private static ArrayList<Im> getIms(String id,
            ContentResolver contentResolver) {
        //ArrayList<Im> IMs = new ArrayList<Im>();
        Cursor imCursor = contentResolver
                .query(ContactsContract.Data.CONTENT_URI,
                        null,
                        ContactsContract.Data.CONTACT_ID + QUESTION_AND
                                + ContactsContract.Data.MIMETYPE + QUESTION,
                        new String[] {
                                id,
                                ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE },
                        null);
        while (imCursor.moveToNext()) {
            String value = imCursor.getString(imCursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
            String protocol = imCursor
                    .getString(imCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Im.PROTOCOL));
            String rowId = imCursor.getString(imCursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Im._ID));
         
Im  imsetget = new Im();
            imsetget.setProtocol(protocol);
            imsetget.setValue(value);
            imsetget.setRowId(rowId);
            imarylst.add(imsetget);
        }
        imCursor.close();
        return imarylst;
    }


here Im is your getter setter class ,as i define below 

public class Im {
    private String protocol = null;
    private String rowId = null;
    private String value = null;
   

    public Im() {
        super();
    }
   
    public Im(String type,String value) {
        super();
        this.protocol = type;
        this.value = value;
    }
   
    public String getRowId() {
        return rowId;
    }
    public void setRowId(String rowId) {
        this.rowId = rowId;
    }
    public String getProtocol() {
        return ContactUtility.replaceNull(protocol);
    }
    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }
    public String getValue() {
        return ContactUtility.replaceNull(value);
    }
    public void setValue(String value) {
        this.value = value;
    }

}



Method to get Organization detail from contact is
 

    private static ArrayList<Organization> getOrganizations(String id,
            ContentResolver contentResolver) {
        Cursor organizationCursor = contentResolver
                .query(ContactsContract.Data.CONTENT_URI,
                        null,
                        ContactsContract.Data.CONTACT_ID + QUESTION_AND
                                + ContactsContract.Data.MIMETYPE + QUESTION,
                        new String[] {
                                id,
                                ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE },
                        null);

        while (organizationCursor.moveToNext()) {
            String name = organizationCursor
                    .getString(organizationCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
            String title = organizationCursor
                    .getString(organizationCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
            String rowId = organizationCursor
                    .getString(organizationCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Organization._ID));
            int type = organizationCursor
                    .getInt(organizationCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Organization.TYPE));
            if (ContactsContract.CommonDataKinds.Organization.TYPE_WORK == type
                    || ContactsContract.CommonDataKinds.Organization.TYPE_OTHER == type) {
          
            
Organization   orgsetget = new Organization();
                orgsetget.setName(name);
                orgsetget.setTitle(title);
                orgsetget.setRowId(rowId);
                orgsetget.setType(String.valueOf(type));
                orgarylst.add(orgsetget);
            }
        }
        contmainsetget.setOrganizations(orgarylst);
        organizationCursor.close();
        return orgarylst;
    }


here Organization is your getter setter class as i define below

public class Organization {
    private String type = null;
    private String title = null;
    private String name = null;
    private String rowId = null;

    public Organization() {
        super();
    }
   
    public Organization(String type,String name,String title) {
        super();
        this.type = type;
        this.name = name;
        this.title = title;
    }

    public String getRowId() {
        return rowId;
    }
    public void setRowId(String rowId) {
        this.rowId = rowId;
    }
    public String getType() {
        return ContactUtility.replaceNull(type);
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getTitle() {
        return ContactUtility.replaceNull(title);
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getName() {
        return ContactUtility.replaceNull(name);
    }
    public void setName(String name) {
        this.name = name;
    }

}


Method to get Phone details form contact id

private static ArrayList<Phone> getPhones(String id,
            ContentResolver contentResolver) {
        Cursor phoneCursor = contentResolver.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + QUESTION,
                new String[] { id }, null);
        while (phoneCursor.moveToNext()) {
            String no = phoneCursor
                    .getString(phoneCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            String rowId = phoneCursor
                    .getString(phoneCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
            int type = phoneCursor
                    .getInt(phoneCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
            if (ContactsContract.CommonDataKinds.Phone.TYPE_WORK == type
                    || ContactsContract.CommonDataKinds.Phone.TYPE_HOME == type
                    || ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE == type
                    || ContactsContract.CommonDataKinds.Phone.TYPE_OTHER == type) {
              
Phone phonesetget = new Phone();
           
                phonesetget.setNo(no);   
                phonesetget.setRowId(rowId);   
                phonesetget.setType(String.valueOf(type));
               
                    phonearylst.add(phonesetget);   
            }
        }
        phoneCursor.close();
        return phonearylst;
    }

here Phone is your getter setter class as i define below

public class Phone {
    private String type = null;
    private String rowId = null;
    private String no = null;

    public Phone() {
        super();
    }

    public Phone(String type,String no){
        super();
        //this.rowId = rowid;
        this.type = type;
        this.no = no;
    }
   
    public String getRowId() {
        return rowId;
    }
    public void setRowId(String rowId) {
        this.rowId = rowId;
    }
    public String getType() {
        return ContactUtility.replaceNull(type);
    }
    public void setType(String type) {
        this.type = type;
    }
   
    public String getNo() {
        return ContactUtility.replaceNull(no);
    }
    public void setNo(String no) {
        this.no = no;
    }
}


Method to get FirstName LastName from contact id
 

 private static String[] getFirstNameLastName(String id,
            ContentResolver contentResolver) {
        String[] retValue = new String[2];
        retValue[0] = "";
        retValue[1] = "";

        Cursor nameCursor = contentResolver
                .query(ContactsContract.Data.CONTENT_URI,
                        null,
                        ContactsContract.Data.MIMETYPE + QUESTION_AND
                                + ContactsContract.RawContactsEntity.CONTACT_ID
                                + QUESTION,
                        new String[] {
                                ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,
                                id }, null);
        while (nameCursor.moveToNext()) {
            String given = nameCursor
                    .getString(nameCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
            String family = nameCursor
                    .getString(nameCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
            if (given != null) {
                retValue[0] = given;
            }
            if (family != null) {
                retValue[1] = family;
            }
        }
        nameCursor.close();
        return retValue;
    }


Method to get Note from contact id

@SuppressLint("NewApi")
    private static Note getNote(String id, ContentResolver contentResolver) {

        Cursor noteCursor = contentResolver
                .query(ContactsContract.Data.CONTENT_URI,
                        null,
                        ContactsContract.Data.CONTACT_ID + QUESTION_AND
                                + ContactsContract.Data.MIMETYPE + QUESTION,
                        new String[] {
                                id,
                                ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE },
                        null);
        if (noteCursor.moveToFirst()) {
            String value = noteCursor
                    .getString(noteCursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
            String rowId = noteCursor.getString(noteCursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Note._ID));
          
Note cont_note = new Note();
            cont_note.setRowId(rowId);
            Log.i(tag, "contact note value ...."+value);
            if(value.isEmpty()){
               
            }else{
                cont_note.setText(value);   
            }
        }
        noteCursor.close();

        return cont_note;
    }


here Note is your getter setter class as i define below

public class Note {
    private String rowId = null;
    private String text = null;

    public Note() {
        super();
    }
   
    public String getText() {
        return ContactUtility.replaceNull(text);
    }
    public void setText(String text) {
        this.text = text;
    }

    public String getRowId() {
        return rowId;
    }
    public void setRowId(String rowId) {
        this.rowId = rowId;
    }

}
 


Display Image from photoId

// contimg is your imageview 

if (photoId != null) {
              
                    Uri photoUri = ContentUris.withAppendedId(
                            ContactsContract.Data.CONTENT_URI,
                            Long.parseLong(photoId));
                    contimg.setImageURI(photoUri);
               
              
            } else {
               // set default image like define below //contimg.setImageResource(R.drawable.ic_contact_picture_holo_light);
            }

No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...