get email type and phone number type 's label used android

ram ji picture ram ji · Aug 4, 2011 · Viewed 9.3k times · Source

I want to fetch the type of email type and phone number type's label used but when im fetching the data using these code its giving position of label use means return integer value but i want the label used.

Where i am wrong in my code?

Success full getting email id but for type giving int. value as 1,2.

So how to get label for type?

public String [] getEmailid(long _id) {
    String emailid = null ;
    String emailType = null ;
    try {
        Cursor cursor = getContentResolver().query(  
            ContactsContract.CommonDataKinds.Email.CONTENT_URI,  
            new String[]{Email.DATA,Email.TYPE},
            ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ _id, 
            // We need to add more selection for phone type
            null,
            null);

        if(cursor != null) {
            while (cursor.moveToNext()) {  
                // This would allow you get several email addresses  
                // if the email addresses were stored in an array  
                // Log.i("RETURN EMAIL TYPA",emailid);
                emailid = cursor.getString(cursor.getColumnIndex(Email.DATA)); 
                emailType = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));  

                // TODO Auto-generated method stub
                if(emailid != null)
                    break;
            }
        }
    }
//.....

Answer

Mason Lee picture Mason Lee · Aug 30, 2011

Common types (home, work, etc.) are stored as ints. This avoids keeping redundant strings in the database and allows for localization. You can look up the localized string for the common type's int with the following API:

http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Email.html#getTypeLabel(android.content.res.Resources,%20int,%20java.lang.CharSequence)

Note that when the type is TYPE_CUSTOM, you need to provide the custom label. Here is an example:

int type = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
String customLabel = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.LABEL));
CharSequence emailType = ContactsContract.CommonDataKinds.Email.getTypeLabel(context.getResources(), type, customLabel);