I am making a small app where I can get the contacts of my phone using a content provider and display them in a listview as illustrated.
I want to select a row of the listview and automically make a phone call to that specific contact. I tried some things,but they don't work. Any ideas? Here is my code.
public class MainActivity extends ListActivity implements AdapterView.OnItemClickListener{
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ContentResolver cr = getContentResolver();
Cursor c = cr.query(ContactsContract.Contacts.CONTENT_URI,
new String[] {ContactsContract.Contacts.DISPLAY_NAME},
null, null, null);
List<String> contacts = new ArrayList<String>();
if (c.moveToFirst()) {
do {
contacts.add(c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
} while (c.moveToNext());
}
adapter = new ArrayAdapter<String>(this, R.layout.activity_main, contacts);
setListAdapter(adapter);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//The answer should be inside here.
}
}
First, ensure that you have added the Permission to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
UPDATE:
On Android 6 and higher stating the permission in the manifest is not enough, you have to explicitly ask user for granting permission on reading contacts otherwise, you will get an exception. See this answer for more details.
Then you can loop through your phone contacts like this:
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Boolean.parseBoolean(hasPhone)) {
// You know it has a number so now query it like this
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));
}
phones.close();
}
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
// This would allow you get several email addresses
String emailAddress = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
emails.close();
}
Try with:
private void doMagicContacts() {
Cursor cursor = getContentResolver()
.query(ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
null);
if (cursor == null) {
return;
}
cursor.moveToFirst();
do {
String name = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String id = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.NAME_RAW_CONTACT_ID));
Cursor phones = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID+" = " + id,
null,
null);
if (phones != null) {
while (phones.moveToNext()) {
String phoneNumber = phones.getString(
phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.d(TAG, "doMagicContacts: " + name + " " + phoneNumber);
}
phones.close();
}
} while (cursor.moveToNext());
cursor.close();
}
Related
I am working on an application that includes dealing with Contacts also. I have tried distinct methods But none of them worked.. I am fetching contact details in one of the tabbed Activity fragments. Its fetching all other details correctly except Email Address of the contact. I have figured out the reason because ContactsContract.CommonDataKinds.Phone._ID fetching different Ids than ContactsContract.CommonDataKinds.Email.CONTACT_ID.. Now what should i code to fetch all the contacts from phone along with name, number, email, photo_uri. Please Help! Here is my piece of code:
public class FragmentContacts extends Fragment{
ListView contacts;
ArrayList<String> namelist=new ArrayList<String>();
ArrayList<String> number=new ArrayList<String>();
ArrayList<String> contact_id=new ArrayList<String>();
ArrayList<String> contact_image_uri=new ArrayList<String>();
ArrayList<String> contact_email=new ArrayList<String>();
String[] sortImage;
ImageButton addContact;
String email;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);
contacts=(ListView)rootView.findViewById(R.id.contact_list);
addContact=(ImageButton)rootView.findViewById(R.id.add_contact);
namelist.clear();
getContacts();
addContact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addNewContact();
}
});
return rootView;
}
public void getContacts() {
ContentResolver cr = getActivity().getApplicationContext().getContentResolver();
Cursor managedCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.PHOTO_URI}, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");
while (managedCursor.moveToNext()) {
String id = managedCursor.getString(0);
String name = managedCursor.getString(1);
String phoneNumber = managedCursor.getString(2);
String image_uri=managedCursor.getString(3);
//email=managedCursor.getString(4);
email=getEmail(id);
System.out.println("Email is "+email);
contact_id.add(id);
// System.out.println("Id is "+id+" "+"Name is "+name);
namelist.add(name);
number.add(phoneNumber);
contact_image_uri.add(image_uri);
contact_email.add(email);
}
managedCursor.close();
ContactAdapter a = new ContactAdapter(getActivity(), contact_id, namelist, number,contact_image_uri,contact_email);
contacts.setAdapter(a);
}
public void addNewContact()
{
Intent in= new Intent(getContext(),AddContact.class);
startActivity(in);
}
private String getEmail(String contactId) {
String mailE=null;
ContentResolver cr = getActivity().getApplicationContext().getContentResolver();
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = ?",
new String[]{contactId}, null);
while (cursor.moveToNext())
{
mailE=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
System.out.println("In Between Email is "+mailE);
}
cursor.close();
return mailE;
}
}
For Getting Email Address Email,
String email = managedCursor .getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Try this.
You can you this method to get the email ID of that contact. In your case you can pass the contact ID directly.
public ArrayList<String> getEmail() {
ArrayList<String> names = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
//to get the contact names
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("Name :", name);
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email", email);
if(email!=null){
names.add(name);
}
}
cur1.close();
}
}
return names;
}
There's some confusion in your code, you can't query over Phone.CONTENT_URI and expect to get an email-address, as emails are stored in Email.CONTENT_URI.
The naive way would be to add another query over Email.CONTENT_URI, but that's not needed, your best choice would be to query over the entire Data.CONTENT_URI table which contains both emails and phones (and a lot of other types of information), and ask for only mimetypes for emails and phones.
String[] projection = new String[] { Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Email.ADDRESS, Phone.NUMBER };
Cursor cur = getContentResolver().query(Data.CONTENT_URI, projection, Data.MIMETYPE + " IN ('" + Email.CONTENT_ITEM_TYPE + "' , '" + Phone.CONTENT_ITEM_TYPE +"')", null, null);
while ((cur != null) && cur.moveToNext()) {
long contactId = cur.getLong(0);
String name = cur.getString(1);
String type = cur.getString(2);
String email = null;
String phone = null;
if (type == Email.CONTENT_ITEM_TYPE) {
email = cur.getString(3);
} else {
phone = cur.getString(4);
}
// do whatever you want with above variables.
}
Note: you'll get the same contactId and name for each phone/email stored for that contact, so you should use a HashMap to map contactId to list of details, in order to store all this data.
You should definitely read first to docs about the ContactsContract API, and Contacts and Data tables, as these are crucial to understanding how to query for information about contacts.
Finally i'v resolved it. I have changed contact_id from ContactsContract.CommonDataKinds.Phone._ID to ContactsContract.CommonDataKinds.Phone.CONTACT_ID... My Updated code is:
public class FragmentContacts extends Fragment{
ListView contacts;
ArrayList<String> namelist=new ArrayList<String>();
ArrayList<String> number=new ArrayList<String>();
ArrayList<String> contact_id=new ArrayList<String>();
ArrayList<String> contact_image_uri=new ArrayList<String>();
ArrayList<String> contact_email=new ArrayList<String>();
ImageButton addContact;
String email;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);
contacts=(ListView)rootView.findViewById(R.id.contact_list);
addContact=(ImageButton)rootView.findViewById(R.id.add_contact);
namelist.clear();
getContacts();
addContact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addNewContact();
}
});
return rootView;
}
public void getContacts() {
ContentResolver cr = getActivity().getApplicationContext().getContentResolver();
Cursor managedCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.PHOTO_URI}, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE NOCASE ASC");
while (managedCursor.moveToNext()) {
String id = managedCursor.getString(0);
String name = managedCursor.getString(1);
String phoneNumber=managedCursor.getString(2);
String image_uri=managedCursor.getString(3);
email=getEmail(id);
contact_id.add(id);
namelist.add(name);
number.add(phoneNumber);
contact_image_uri.add(image_uri);
contact_email.add(email);
}
managedCursor.close();
ContactAdapter a = new ContactAdapter(getActivity(), contact_id, namelist, number,contact_image_uri,contact_email);
contacts.setAdapter(a);
}
public void addNewContact()
{
Intent in= new Intent(getContext(),AddContact.class);
startActivity(in);
}
private String getEmail(String contactId) {
String mailE=null;
ContentResolver cr = getActivity().getApplicationContext().getContentResolver();
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = ?",
new String[]{contactId}, null);
while (cursor.moveToNext())
{
mailE=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
}
cursor.close();
return mailE;
}
}
i use of this code that can get contact's number and contact's thumbnail , but this is not suitable and take to long to load and prepare contacts .
please help me to improve performance of this code :
List<Contact_Pojo> list = new ArrayList<Contact_Pojo>();
ContentResolver cr = getActivity().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, Phone.DISPLAY_NAME + " ASC");
int i = 0;
if (cur.moveToFirst()) {
while (cur.moveToNext()) {
Cursor phoneCursor = getActivity()
.getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER, },
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?",
new String[] { cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID)) },
null);
if (phoneCursor.moveToFirst()) {
Contact_Pojo personContact = new Contact_Pojo();
/*
* Id
*/
personContact.setId(cur.getString(1));
/*
* Name
*/
personContact
.setName(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
/*
* Photo ID
*/
personContact
.setImageUrl(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI)));
/*
* Number
*/
personContact.setNumber(phoneCursor.getString(0));
//
list.add(personContact);
}
phoneCursor.close();
}
}
cur.close();
return list;
Update 4/4/2015
it is slow because it has getting all column from contact provider , in this table has +20 column that take many time .
How about using AsyncTaskLoader,
write this code when you want to get contacts list
getLoaderManager() or getSupportLoaderManager().initLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> loadingCallback)
public class LoadingCallback implements LoaderManager.LoaderCallbacks<Void> {
#Override
public Loader<Void> onCreateLoader(int i, Bundle bundle) {
// Show your dialog;
return new InitializeContactsTask(context, (ArrayList) contactList, mAdapter);
}
#Override
public void onLoadFinished(Loader<Void> voidLoader, Void aVoid) {
// UI Work here after background task and hide dialog.
}
#Override
public void onLoaderReset(Loader<Void> voidLoader) {
}
}
and
public class InitializeApplicationsTask extends AsyncTaskLoader<Void> {
#Override
protected void onStartLoading() {
forceLoad();
}
#Override
public Void loadInBackground() {
// Query the contacts here and return null
return null;
}
}
It's slow because, you are getting all column from contact provider, so use projection to get only those column which are required. like
String[] projection = {
ContactsContract.Contacts.DISPLAY_NAME
// this is just example, add fields that required
} ;
can you please tell me what is purpose of second query ? because here is my result of getting contact
and my method for getting all contact is
#SuppressLint("InlinedApi")
public void readContacts() {
long l1 = System.currentTimeMillis();
Log.d("DEBUG",
"starting readContacts() time " + l1);
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
Contact_pojo cp = new Contact_pojo();
String Contact_Id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String Contact_Name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String photothumb = "";
try {
photothumb = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
if (photothumb != null
&& !photothumb.toString().equalsIgnoreCase("")) {
cp.setImageUri(photothumb);
}
} catch (Exception e) {
e.printStackTrace();
}
cp.setId(Contact_Id);
cp.setName(Contact_Name);
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + Contact_Id, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones
.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
Log.d("DEBUG", "number " + number + " type " + type);
cp.setNumber(number);
}
phones.close();
contactList.add(cp);
}
}
long l2 = System.currentTimeMillis();
Log.d("DEBUG",
"Finished readContacts() time " + l2);
Log.d("DEBUG","Total contact loaded "+contactList.size()+" within "+ (l2 - l1) + "ms");
}
Don't use double query, this is taking too much time if device have lots of contacts and it's not working in some device also...
You can also fetch contact with single query..
Use below code..
public void fetchContacts()
{
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID};
String _ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String DISPLAY_NAME = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
Cursor cursor = mActivity.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER + "=?", new String[] { "1" },
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
System.out.println("contact size.."+cursor.getCount());
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));
String phoneNumber = cursor.getString(cursor.getColumnIndex(NUMBER));
System.out.println(" name : "+name);
System.out.println(" number : "+phoneNumber);
}
}
}
Check below links for get contact photo.
ContactsContract.Contacts.Photo
Getting a Photo from a Contact
I am using android 2.1 platform.The code I have will display the Name number and email ID of all the persons from the Emulator contact list,by using this code I will get the requirement as on above.I have some contacts in my emulator, I created two more contacts (say C and D). BUT the issues are
Issue 1. If I create a new contact having name and number not email, This contact will take the email ID from the contact just down to it.for eg: if I create a name C has no email ID but D has email ID, D is the contact already in the Emulator. C will take D's email ID.C and D have same email ID.
Issue 2. If again I create a new contact having name email but not number, This contact will take the number from the contact I previously created.for eg: if I create the name E has no number but it will take the number of the contact C. so C and E have the same number .
I am using this code
public class GetAllDatas extends Activity {
ListView lvItem;
private Button btnAdd;
String displayName="", emailAddress="", phoneNumber="";
ArrayList<String> contactlist=new ArrayList<String>();
ArrayAdapter<String> itemAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvItem = (ListView)this.findViewById(R.id.lvitems);
btnAdd = (Button)this.findViewById(R.id.btnAddItem);
itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactlist);
lvItem.setAdapter(itemAdapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
readContacts();
}
});
}
private void readContacts()
{
ContentResolver cr =getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext())
{
displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
while (emails.moveToNext())
{
emailAddress = emails.getString(emails.getColumnIndex(Email.DATA));
break;
}
emails.close();
if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
while (pCur.moveToNext())
{
phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
break;
}
pCur.close();
}
// To display the Details
contactlist.add(displayName+", "+phoneNumber+", "+ emailAddress+"\n");
itemAdapter.notifyDataSetChanged();
}
Collections.sort(contactlist);
cursor.close();
}
}
How can I solve this problem ?
As i Understand your question..
Before add new item in reference variables just erase the previous one..
like,
emailAddress = "" ;
displayName = "" ;
phoneNumber = "" ;
in your while loop for fetching contact information and adding it to list..
private void readContacts()
{
ContentResolver cr =getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext())
{
emailAddress = "" ;
displayName = "" ;
phoneNumber = "" ;
displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
while (emails.moveToNext())
{
emailAddress = emails.getString(emails.getColumnIndex(Email.DATA));
break;
}
emails.close();
if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
while (pCur.moveToNext())
{
phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
break;
}
pCur.close();
}
// To display the Details
contactlist.add(displayName+", "+phoneNumber+", "+ emailAddress+"\n");
}
Collections.sort(contactlist);
itemAdapter.notifyDataSetChanged();
cursor.close();
}
try this one ..........
public void readContacts(){
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
contactName=new String[cur.getCount()];
contactNumber=new String[cur.getCount()][];
contactEmail=new String[cur.getCount()][];
int i=0;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contactName[i]=name;
Log.e("Contact Name",contactName[i]);
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
contactNumber[i]=new String[pCur.getCount()];
int j=0;
while (pCur.moveToNext()) {
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactNumber[i][j]=phone;
Log.d("Contact number",contactNumber[i][j]);
j++;
}
pCur.close();
}
// get email and type
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
contactEmail[i]=new String[emailCur.getCount()];
int k=0;
while (emailCur.moveToNext()) {
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
contactEmail[i][k]=email;
Log.v("Contact email",contactEmail[i][k]);
k++;
}
emailCur.close();
}
} i++;
}
I want to get several contact details from a contact list view. I have this code:
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {
//HERE I WANT TO GET CONTACT DETAILS FROM THE ID PARAMETER
Uri lookupUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, Uri.encode(id));
Cursor c = getContentResolver().query(lookupUri, new String[]{Contacts.DISPLAY_NAME}, null,null,null);
try {
c.moveToFirst();
String displayName = c.getString(0);
} finally {
c.close();
}
}
But I get this exception: IllegalArgumentException, Invalid lookup id (when I call query method from cursor).
So I dont know how to get a valid lookup id from the item list.
Any idea? thanks!
Here id means, A contact id for which you want to fetch the contact details,
The simply code for getting phone number for a particular contact id is like,
// Build the Uri to query to table
Uri myPhoneUri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id);
// Query the table
Cursor phoneCursor = managedQuery(
myPhoneUri, null, null, null, null);
// Get the phone numbers from the contact
for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor.moveToNext()) {
// Get a phone number
String phoneNumber = phoneCursor.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append("Phone: " + phoneNumber + "\n");
}
}
So, from your question I have to doubt for id parameter which are you sing in your uri just clear that, Also in my example the id is string type...
Hope you will understand it.
Update: Uri.encode(id) instead of just pass id in string format.
Thanks..
Because managedQuery is deprecated you can also use
Cursor phoneCursor = getContentResolver().query(myPhoneUri, null, null, null, null);
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int columnIndex = arg2.getColumnIndex(ContactsContract.Contacts._ID);
private void retriveContactImage(ImageView imageView, int columnIndex) {
Bitmap photo = null;
try {
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(columnIndex)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(photo);
}
private ArrayList<String> retrieveContactNumbers(TextView textView, long contactId) {
ArrayList<String> phoneNum = new ArrayList<String>();
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactId + "" }, null);
if (cursor.getCount() >= 1) {
while (cursor.moveToNext()) {
// store the numbers in an array
String str = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (str != null && str.trim().length() > 0) {
phoneNum.add(str);
}
}
}
cursor.close();
textView.setText(phoneNum.get(0));
return phoneNum;
}
private void retrieveContactName(TextView textView, long contactId) {
String contactName = null;
Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_URI, new String[] { ContactsContract.Contacts.DISPLAY_NAME },
ContactsContract.Contacts._ID + " = ?", new String[] { String.valueOf(contactId) }, null);
if (cursor.moveToFirst()) {
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
cursor.close();
textView.setText(contactName);
}
How would I filter the returned contacts by the groupName?
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
String groupName = parent.getItemAtPosition(pos).toString();
Log.i(this.toString(), String.format("Show Group: %s", groupName));
Cursor cur = this.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (name != null) {
Log.i(this.toString(), String.format("Show: %s", name));
}
}
cur.close();
}
}
I was able to filter contacts by group by using the following query:
Cursor c = this.managedQuery(Data.CONTENT_URI, new String[] {
Contacts.DISPLAY_NAME, Contacts._ID },
GroupMembership.GROUP_ROW_ID + " = ?",
new String[] { groupId }, Phone.TIMES_CONTACTED + " DESC");
Obviously you need to know the Group Id in order to complete this, how I did this was used a SimpleCursorAdapter to a Spinner w/the following Cursor:
Cursor c = managedQuery(Groups.CONTENT_URI,
new String[] { Groups.SYSTEM_ID, Groups._ID,},
Groups.SYSTEM_ID + " NOT NULL",
null, null);
This works for me for getting all group IDs rather than just system IDs:
public HashMap<String, Long> fillGroupsHashMap(HashMap<String, Long> groups) //
{
Cursor tempCur = activity.managedQuery(Groups.CONTENT_URI,
new String[] { Groups._ID, Groups.TITLE }, null, null, null);
tempCur.moveToFirst();
while (tempCur.moveToNext()) //
{
groups.put(tempCur.getString(1), tempCur.getLong(0));
}
return groups;
}
Unfortunately only System Groups ("groups that have a special meaning to the sync adapter") have SYSTEM_IDs. So this method is not bullet proof.
I had a heachache with this, but finally i get this working with whis code, and now i can filter by only phone numbers and avoid repeated and unused groups:
String[] GROUP_PROJECTION = new String[] {
ContactsContract.Groups._ID, ContactsContract.Groups.TITLE,ContactsContract.Groups.SUMMARY_COUNT ,ContactsContract.Groups.SUMMARY_WITH_PHONES };
Cursor c = context.getApplicationContext().getContentResolver().query(
ContactsContract.Groups.CONTENT_SUMMARY_URI,
GROUP_PROJECTION,
ContactsContract.Groups.SUMMARY_WITH_PHONES +"> 0"
, null, ContactsContract.Groups.TITLE + " ASC");