理解MimeType:根据查询条件的MimeType 决定Data表的data1~data15的值。
对于MimeType的值有CommonDataKinds.xxxx.CONTENT_ITEM_TYPE。查询的得到的结果为
CommonDataKinds.xxxx.xxx。
- public class ContactDataModel {
- private Context mContext;
- private ContentResolver mContentResolver;
- public ContactDataModel(Context mContext) {
- this.mContext = mContext;
- mContentResolver=mContext.getContentResolver();
- }
- public List<HashMap<String,String>> getContactsByName(String mName){
- mName=mName.trim();
- List<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();
- boolean ifQueryAll=false;
- Cursor cn =null;
- Cursor cu= null;//cu姓名游标,cn电话游标
- String selection=null;//查询条件
- ifQueryAll=mName.equals("")?true:false;
- //根据ifQuery判断查询条件
- if (ifQueryAll) {
- selection=Data.MIMETYPE+"='"+CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE+"'";
- }else{
- selection=Data.MIMETYPE+"='"+CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE+"' AND "+
- CommonDataKinds.StructuredName.DISPLAY_NAME+"LIKE '%'"+mName+"%'";
- }
- //根据姓名查询出通讯录ID与完整姓名。
- cu=mContentResolver.query(Contacts.CONTENT_URI,new String[]{Data.RAW_CONTACT_ID,StructuredName.DISPLAY_NAME},
- selection, null, null);
- //根据通讯录ID查询电话号码的条件
- selection=Data.MIMETYPE+"='"+CommonDataKinds.Phone.CONTENT_ITEM_TYPE+"' AND "+ Data.RAW_CONTACT_ID+" =? ";
- //执行查询
- while (cu.moveToNext()) {
- String mContactId=String.valueOf(cu.getInt(0));
- cn=mContentResolver.query(Contacts.CONTENT_URI, new String[]{CommonDataKinds.Phone.NUMBER},
- selection, new String[]{mContactId}, null);
- while (cn.moveToNext()) {
- HashMap<String,String> map=new HashMap<String, String>();
- map.put("display_name",cu.getString(1));
- map.put("phone_number",cn.getString(0));
- list.add(map);
- }
- }
- cu.close();
- cn.close();
- return list;
- }
- }