我检索联系人列表(姓名,电话号码)的目的记录在一个json对象中,以通过Web服务发送到服务器
String id, name;
ContentResolver crs = getContentResolver();
Cursor people = crs.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
String phone = "";
people.movetoPosition(Contexts.getnbContacts());
while (people.movetoNext())
{
id = people.getString(people
.getColumnIndex(ContactsContract.Data._ID));
name = people.getString(people
.getColumnIndex(ContactsContract.Data.disPLAY_NAME));
if (Integer
.parseInt(people.getString(people
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + id, null, null);
while (phones.movetoNext()) {
// pdata =
// phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
phone = phone
+ phones.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA))
+ " and ";
;
}
phones.close();
}
我的问题是:如何将每个联系人(姓名,电话号码)保存在数组或arraylist中…
每个表行指定联系人
我认为数组类型与json格式兼容
那么如何将联系人复制到json对象中
解决方法:
这是我的代码
public static Map<String, String> getAddressBook(Context context)
{
Map<String, String> result = new HashMap<String, String>();
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while(cursor.movetoNext())
{
int phone_idx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int name_idx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.disPLAY_NAME);
String phone = cursor.getString(phone_idx);
String name = cursor.getString(name_idx);
result.put(name, phone);
}
cursor.close();
return result;
}
和需要
<uses-permission android:name="android.permission.READ_CONTACTS"/>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。