getting phone book to application
I wrote the following code to get the contact details. The problem is the phone number I am getting is showing null. Can You help me?
private void displayRecords() {
// An array specifying which columns to return.
String columns[] = new String[] { People.NAME, People.NUMBER_KEY};
Uri mContacts = People.CONTENT_URI;
Cursor cur = managedQuery(mContacts, columns, // Which columns to return
null, // WHERE clause; which rows to return(all rows)
null, // WHERE clause selection arguments (none)
null // Order-by clause (ascending by name)
);
if (cur.moveToFirst()) {
String name = null;
String phoneNo = null ;
do {
// Get the field values
name = cur.getString(cur.getColumnIndex(People.NAME));
phoneNo =cur.getString(cur.getColumnIndex(People.NUMBER_KEY));
Toast.makeText(this, name + " 开发者_JS百科" + phoneNo, Toast.LENGTH_LONG).show();
} while (cur.moveToNext());
}
}
try this code it will helpful for u
people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int position=0;
Cursor q=db.query(mProfile,new String[]{"person_name"},"person_name"+"!='"+null+"'",null,null, null, null);
people.moveToFirst();
int nameFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
while(!people.isAfterLast()) {
Cursor o=db.query(mProfile,new String[]{"person_name"},"person_name"+"='"+people.getString(nameFieldColumnIndex)+"'",null,null, null, null);
if(!(o.getCount()>0))
{
mConname.add(position, people.getString(nameFieldColumnIndex));
try{
String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ( hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
mConno.add(position,phoneNumber);
}
phones.close();
}
if(hasPhone=="false")
{ mConname.remove(position);
}
else
position++;
}
catch(Exception e)
{
}
}
people.moveToNext();
}
精彩评论