【发布时间】:2015-09-22 18:14:18
【问题描述】:
这是我的代码,它提供了我所有的 Android 设备号码,例如手机号码、whatsapp 号码和固定电话号码。但我只想将安卓电话簿中的手机号码和座机号码放入我的应用程序中。如何只获取每个联系人姓名的手机号码和座机号码?
private void addContactsInList() {
// TODO Auto-generated method stub
Cursor cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
try {
ContactsListClass.phoneList.clear();
} catch (Exception e) {
}
while (cursor.moveToNext()) {
String phoneName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int phoneId = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Contact cp = new Contact();
cp.setName(phoneName);
cp.setNumber(phoneNumber);
cp.setId(phoneId);
ContactsListClass.phoneList.add(cp);
// Log.e("add to list", "content" + phoneId +phoneName +phoneNumber);
}
cursor.close();
lv = new ListView(context);
lv.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
llContainer.addView(lv);
Collections.sort(ContactsListClass.phoneList, new Comparator<Contact>() {
@Override
public int compare(Contact lhs,
Contact rhs) {
return lhs.getName().compareTo(
rhs.getName());
}
});
contactAdapter = new ContactsAdapter(MainActivity.this,
ContactsListClass.phoneList);
lv.setAdapter(contactAdapter);
【问题讨论】:
-
@brennanyoung,我怎样才能在我的代码中获得这三种类型的内容,你能帮我吗?请
-
见
ContactsContract.CommonDataKinds.Phone.TYPE专栏 -
@ pskink , String phoneNumber = cursor.getString(cursor.getColumnIndex(String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)) 这给了我错误
-
列名是
ContactsContract.CommonDataKinds.Phone.TYPE而不是ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE