sqlite android提取联系人与电话号码
发布时间:2020-12-12 19:00:26 所属栏目:百科 来源:网络整理
导读:我正面临这个sqlite问题,我试图通过以下查询提取具有电话号码的联系人: Cursor cursor = context.getContentResolver(). query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,new String[]{ ContactsContract.CommonDataKinds.Phone.CONTACT_ID,Cont
我正面临这个sqlite问题,我试图通过以下查询提取具有电话号码的联系人:
Cursor cursor = context.getContentResolver(). query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,new String[]{ ContactsContract.CommonDataKinds.Phone.CONTACT_ID,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts.PHOTO_URI },ContactsContract.Contacts.HAS_PHONE_NUMBER + ">?",new String [] {"0"},ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC" ); 问题是,如果联系人有超过1个电话号码,结果将采用以下形式: id: 451,name: Maria,photoUri: null,has_phone_number: 1,phone_number: 0700 000 000 id: 451,phone_number: 0800 000 000 id: 451,phone_number: 0900 000 000 由于重复数据,这是不合需要的. 我想只对数据库进行一次查询,希望可以写入以返回结果,如下所示: id: 451,phone_number: 0700 000 000,0800 000 000,0900 000 000 可能吗? 谢谢. 解决方法我不认为你想做什么,这是一个使用HashMap的解决方法.它可以处理数千个条目,所以不要担心.Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,new String[]{ ContactsContract.CommonDataKinds.Phone.CONTACT_ID,ContactsContract.Contacts.PHOTO_URI },ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); Map<String,List<String>> phonesPerContact = new HashMap<>(); if (cursor.moveToFirst()) { do { String name = cursor.getString(2); String phone = cursor.getString(1); if (!phonesPerContact.containsKey(name)){ phonesPerContact.put(name,new ArrayList<String>()); } phonesPerContact.get(name).add(phone); } while (cursor.moveToNext()); } for (String name: phonesPerContact.keySet()){ //Do whatever you want with the contact and its list of phones List<String> phones = phonesPerContact.get(name); Log.i("test","Name: " + name + ",Numbers: " + phones.toString()); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |