我想获取电子邮件类型和电话号码类型的标签类型,但是当使用这些代码获取数据时,它的给定位置的标签使用意味着返回整数值,但我想要使用的标签.
我的代码在哪里错了?
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; } } } //.....
解决方法
常用类型(家庭,工作等)以int形式存储.这样可避免在数据库中保留冗余字符串并允许本地化.您可以使用以下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);