Reputation: 142
I'm having trouble finding out how to query a custom phone number type. Like if you go into contacts-edit contact and change one of the phone numbers to a custom label. Is there a way to get the name entered into the custom type label?
I have tried
"android.content.res.Resources.getSystem() .getStringArray( android.R.array.phoneTypes)"
but it just seems to crash the app and I think its for an old version of android.
And I have also tried
curser.getString(curser.getColumnIndex(ContactsContract.CommonDataKinds.Phone .LABEL)".
If anyone has an idea it would be greatly appreciated, or maybe point me to a duplicate if one exist I couldn't find one though.
Upvotes: 2
Views: 4023
Reputation: 1559
In this way, in typeName
you will get the standard or the custom label. (crPhones
is the cursor that iterate the numbers associated to a single contact):
String label = crPhones.getString(crPhones.
getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
int type = crPhones.getInt(crPhones.
getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String typeName = ContactsContract.CommonDataKinds.Phone.
getTypeLabel(context.getResources(), type, label).toString();
Upvotes: 1
Reputation: 595
This is my code.
First, get type and label.
and get label using getTypeLabel function.
fun getPhoneNumbers(contactId: String): ArrayList<ContactNumber> {
val result = ArrayList<ContactNumber>()
/*///////////////////////////get type and custom label*/
val phoneFetchCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
arrayOf(ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.LABEL),
ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID + "=" + contactId, null, null)
while (phoneFetchCursor.moveToNext()) {
val num = phoneFetchCursor.getString(phoneFetchCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
val typ = phoneFetchCursor.getInt(phoneFetchCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))
val label = phoneFetchCursor.getString(phoneFetchCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL))
/*///////////////////////////getTypeLabel's third parameter is custom label.
when it is null then it returns localized default label.*////////////////////////
val typName = ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.resources, typ!!, label).toString()
var contactNumber = ContactNumber(contactId, num, typ,typName)
result.add(contactNumber)
}
phoneFetchCursor.close()
return result
}
Upvotes: 0
Reputation: 458
right now I've been doing the same thing, after ensuring your queries are correct (i'm querying ContactsContract.CommonDataKinds.Phone.CONTENT_URI) (make sure your projection's are right, etc.) you can do something like below, i guess the difficulty you're facing is picking up a preset label vs. a custom label. preset labels are represented as integers in the TYPE column whereas if the TYPE == TYPE_CUSTOM, the LABEL field will have the data you're looking for.
moving from numbers in TYPE to a string is with a method provided. i'm not sure about the localization though i think that's handled.
Cursor curse = mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.LABEL},
ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?", new String[]{numnum}, null);
int colIndex = curse.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int lblIndex = curse.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL);
ArrayList<String> numbers = new ArrayList<String>();
String cur = "";
while(curse.moveToNext())
{
int labelType = curse.getInt(colIndex);
if(labelType == ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM)
{
cur = curse.getString(lblIndex);
}
else
{
CharSequence seq = ContactsContract.CommonDataKinds.Phone.getTypeLabel(mContext.getResources(), labelType, "Mobile");
cur = seq.toString();
}
numbers.add(cur);
}
curse.close();
at the end of this snippet you'll end up with an arraylist of strings filled with the labels used for this phone number. note that the phone number needs to be a pretty precise match, so 444-4444 will not match up with 4444444 and vice versa.
personally, i haven't had time to experiment with what the difference is between putting "Mobile" or "" on the last variable in getTypeLabel though it didn't appear to make a difference just yet.
hope this answer wasn't too late.
Upvotes: 3