当前位置: 代码迷 >> Android >> 为什么小弟我或取不到ContendProvider里联系人的号码
  详细解决方案

为什么小弟我或取不到ContendProvider里联系人的号码

热度:68   发布时间:2016-05-01 13:06:02.0
为什么我或取不到ContendProvider里联系人的号码?
代码是这样:
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);

String hisName = cursor.getString(nameFieldColumnIndex);
String hisNumber = cursor.getString(numberFieldColumnIndex);

cursor.close();


问题就出在int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);返回过来的始终是-1,但是获取联系人名字的代码返回都是正常的,求救~~~~sdk是2.2的

------解决方案--------------------
我用老版本的代码可以:
Like this:
Cursor cur = activity.getContentResolver().query(People.CONTENT_URI,
null, null, null, null);
while (cur.moveToNext())
{
int columnIndex = cur.getColumnIndex(People.NAME);
String name = cur.getString(columnIndex);
columnIndex = cur.getColumnIndex(People._ID);
String id = cur.getString(columnIndex);
// 其它的都差不多
}
------解决方案--------------------
android2.1取得通讯录联系人名字和电话号码

Java code
// 取得ContentResolver对象              ContentResolver cr = getContentResolver();                // 取得通讯录的光标              String orderBy = PhoneLookup.DISPLAY_NAME + " COLLATE LOCALIZED ASC";           Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, orderBy);                // 遍历通讯录           ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();           for(int i=0; i<cursor.getCount() ;i++)           {               HashMap<String, Object> map = new HashMap<String, Object>();                              cursor.moveToPosition(i);                              // No.               map.put(COLUMN_ID, i + 1);                              // 取得联系人名字                  int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);                  String name = cursor.getString(nameFieldColumnIndex);                map.put(COLUMN_NAME, name);                              // 取得联系人ID                  String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));                  Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,                       null,                       ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,                       null, null);                               String number = "";               // 取得电话号码(可能存在多个号码)                  for(int j = 0; j < phone.getCount(); j++)                 {                      phone.moveToPosition(j);                   String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                      if (j > 0) {                       number += " , ";                     }                   number += strPhoneNumber;                }                  map.put(COLUMN_NUMBER, number);               Log.d(TAG, "number = " + number);               phone.close();                               listItem.add(map);           }           cursor.close();                      // 生成适配器的Item和动态数组对应的元素           SimpleAdapter listItemAdapter = new SimpleAdapter(this,                   listItem,// 数据源                   R.layout.list_item,// ListItem的XML实现                   // 动态数组与ListItem对应的子项                   new String[] { COLUMN_ID, COLUMN_NAME, COLUMN_NUMBER },                      new int[] { R.id.TextView1, R.id.TextView2, R.id.TextView3 });           lv.setAdapter(listItemAdapter);         }
------解决方案--------------------
最近一直被这个问题所困惑。
  相关解决方案