我想获取所使用的email type
和phone number type's
标签的类型,但是当我使用这些代码获取数据时,其给定标签使用位置意味着返回integer
值,但我想要使用的标签。
我的代码中哪里错了?
成功完全获取电子邮件ID,但类型为int
。值为1,2。
那么如何获得类型的标签?
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;
}
}
}
//.....
答案 0 :(得分:18)
常见类型(家庭,工作等)存储为整数。这避免了在数据库中保留冗余字符串并允许本地化。您可以使用以下API查找公共类型的int的本地化字符串:
请注意,当类型为TYPE_CUSTOM时,您需要提供自定义标签。这是一个例子:
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);
答案 1 :(得分:7)
尝试以下代码。我能够通过电话号码获得Label。
Uri uri = ContactsContract.Contacts.CONTENT_URI;
ContentResolver cr = getContentResolver();
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cur=cr.query(uri, null, null, null, sortOrder);
if(cur.getCount()>0){
while(cur.moveToNext()){
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))> 0) {
//get the phone number
Cursor phoneCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
while (phoneCur.moveToNext()) {
String phoneNumber= phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int phonetype = phoneCur.getInt(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String customLabel = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
String phoneLabel = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(this.getResources(), phonetype, customLabel);
Log.e(TAG, "Phone Number: " + phoneNumber + " Selected Phone Label: " + phoneLabel);
}phoneCur.close();
}
}
} cur.close();
答案 2 :(得分:2)
type = cursor.getInt(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.TYPE));
getActivity().getResources().getString(ContactsContract.CommonDataKinds.Phone.getTypeLabelResource(type))