0

I want to get Contact Image, Name, Number from my mobile using android. im using ContactsContract to fetch these information. but i can't. can anyone know the exact tutorial for learning. i want to list these info in a custom listview. Thanks in Advance. i hope your valuable guiding will help me.

PS: In the DB i have my friends number. i want to synchronize the phone contact and the DB contact using the matched phone no. and i need to display all the matched contacts in the listview. the listview have imageView, name, number..

4

1 回答 1

0
setReminder.numbers.clear();
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    ((Activity) mcontext).startActivityForResult(intent, 1);

并在活动结果上执行此操作

if (requestCode == 1 && resultCode == Activity.RESULT_OK)
  {         
        getContactInfo(intent);         
      }
} 

protected void getContactInfo(Intent intent)
{
    String phoneNumber = "";
    //String email="";

   Cursor cursor =  ((Activity) mcontext).managedQuery(intent.getData(), null, null, null, null);      
   while (cursor.moveToNext()) 
   {           
           String contactId = cursor.getString(cursor.getColumnIndex(BaseColumns._ID));

           String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));


       if ( hasPhone.equalsIgnoreCase("1"))
           hasPhone = "true";
       else
           hasPhone = "false" ;

       if (Boolean.parseBoolean(hasPhone)) 
       {
        Cursor phones = ((Activity) mcontext).getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
        //Cursor Email = ((Activity) mcontext).getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ contactId,null, null);
        while (phones.moveToNext()) 
        {
          phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
          numbers.add(phoneNumber);
        }

        phones.close();
        Cursor emailCur = ((Activity) mcontext).getContentResolver().query( 
                ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
                null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ contactId, 
                null, null); 


            while (emailCur.moveToNext()) { 
                // This would allow you get several email addresses
                    // if the email addresses were stored in an array
                 email = emailCur.getString(
                              emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                String emailType = emailCur.getString(
                              emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
            } 
            emailCur.close();

            if(email==null)
            {
                email="";
            }
       }

      }          
   if(phoneNumber != null && phoneNumber.length() > 0){

       chooseContactArray.clear();
       for(int i=0;i<numbers.size();i++)
       {
           chooseContactArray.add(numbers.get(i));
       }
       adapter.notifyDataSetChanged();
   }
   cursor.close();

}
于 2012-06-14T11:26:31.323 回答