I'm trying to get contact data once a contact name has been clicked from my list view. As per the code below, I can log successfully the ID of my contact, but haven't managed to use the ContactsContract to retrieve the data. What's the best way to do this? (have tried Retrieve Contact Phone Number From URI in Android to not much avail)
EDIT 2 : Fixed code, now works
public void onCreate(Bundle savedInstanceState)
{
Log.v(TAG, "Activity State: onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_manager);
mContactList = (ListView) findViewById(R.id.contactList);
populateContactList();
mContactList.setOnItemClickListener(new OnItemClickListener() {
String strid = Long.toString(id);
Cursor result = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID +" = ?", new String[]{strid}, null);
if (result.moveToFirst()) {
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
Data.RAW_CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
new String[] {String.valueOf(strid)}, null);
if(c.moveToFirst()){
int phoneColumn = c.getColumnIndex("data1");
String phoneNumber = c.getString(phoneColumn);
Log.d("DATA",phoneNumber);
}
}
});
}
EDIT 1 : forgot some important stuff. The code is adapted from the ContactManager example from the Android dev site.
/**
* Populate the contact list based on account currently selected in the account spinner.
*/
private void populateContactList() {
// Build adapter with contact entries
Cursor cursor = getContacts();
String[] fields = new String[] {
ContactsContract.Data.DISPLAY_NAME
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
fields, new int[] {R.id.contactEntryText});
mContactList.setAdapter(adapter);
}
/**
* Obtains the contact list for the currently selected account.
*
* #return A cursor for for accessing the contact list.
*/
private Cursor getContacts()
{
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = null;
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
Assuming you have a valid contactID, you can do this:
Cursor result = managedQuery(ContactsContract.Contacts.CONTENT_URI, null,
ContactsContract.Contacts._ID +" = ?",
new String[]{contactID}, null);
if (result.moveToFirst()) {
for(int i=0; i< result.getColumnCount(); i++){
Log.i("CONTACTSTAG", result.getColumnName(i) + ": "
+ result.getString(i));
}
}
You will have to change the ContactsContract.Contacts.CONTENT_URI and the where clause to the table that you are querying. The above code will print out a bunch of general info about a contact.
Related
I have a SearchView in which if I type a name, it shows a autocomplete list of names matching it. However contacts like Facebook ambulance and AL Cricket also come in these results. How to exclude such results and only get those contacts that are of actual people?
Code I am using to get display name is this :
private String getDisplayNameForContact(Intent intent) {
Cursor phoneCursor = getContentResolver().query(intent.getData(), null, null, null, null);
phoneCursor.moveToFirst();
int idDisplayName = phoneCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String name = phoneCursor.getString(idDisplayName);
phoneCursor.close();
return name;
}
You need to add a selection and selection arguments to your contentResolver.query.
The arguments you want are:
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = ?";
String[] selectionArgs = {"1"};
You add them like so:
ContentResolver contentResolver = getActivity().getContentResolver();
Cursor contactsCursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, selection, selectionArgs, ContactsContract.Contacts.DISPLAY_NAME_PRIMARY);
The constant ContactsContract.Contacts.IN_VISIBLE_GROUP determines what subgroup of contacts you are querying.
As you can see, when you call - query(intent.getData(), null, null, null, null) - get all list data. Where is null, theare is your selections parameters. It remains only to choose the appropriate options. Try my example, and swith different values (ContactsContract.CommonDataKinds.SomethingElse).
I hope this helps.
private static Cursor allContactsQuery(Context context) {
final String[] CONTACTS = new String[]{
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.LABEL,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
ContactsContract.Contacts.LOOKUP_KEY,
};
String SELECTION = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY +
"<>''" + " AND " + ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1" +
" AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1";
final String[] SELECTION_ARGS = null;
final String SORT_ORDER = ContactsContract.Contacts.SORT_KEY_PRIMARY;
Cursor cursor = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
CONTACTS,
SELECTION,
SELECTION_ARGS,
SORT_ORDER);
return cursor;
}
I'm creating a contact app in android and I'm adding contacts to a specific account. How can I select all Data rows for all RawContacts belonging to this account in one query?
I know I can first select all RawContacts belonging to the account and then foreach _ID I could select all Data entries. But first of all that's really slow, but most important I want to get a cursor for all those Data rows so that I can provide this cursor to a CursorAdapter.
Cursor rawCursor = context.getContentResolver().query(
RawContacts.CONTENT_URI,
new String[] {
RawContacts.CONTACT_ID,
RawContacts._ID,
},
RawContacts.ACCOUNT_TYPE + " = ? AND " + RawContacts.ACCOUNT_NAME + " = ? ",
new String[] {account_type, account_name},
null);
while (rawCursor.moveToNext()) {
long rawContactID = rawCursor.getLong(rawCursor.getColumnIndex(RawContacts._ID));
Cursor dataCursor = mContext.getContentResolver().query(
Data.CONTENT_URI,
new String[] {
Data._ID,
Data.MIMETYPE,
Data.IS_SUPER_PRIMARY,
Data.DATA1,
Data.DATA2,
Data.DATA3,
},
Data.RAW_CONTACT_ID + " = ?",
new String[] {String.valueOf(rawContactID)},
null);
}
For now I solved this by first querying all raw_contacts and then using an IN statement in the selection. like:
ArrayList<String> ids = new ArrayList<String>();
Cursor idCursor = getRawContactsCursor(context, new String[]{RawContacts._ID}, account_name);
try {
while (idCursor.moveToNext()) {
ids.add(idCursor.getString(idCursor.getColumnIndex(RawContacts._ID)));
}
} finally {
idCursor.close();
}
String idSql = "?";
for (int i = 1; i < ids.size(); i++) {
idSql += ",?";
}
String whereSQL = Data.RAW_CONTACT_ID + " IN (" + idSql + ")";
String[] whereArgs = null;
if (!TextUtils.isEmpty(mimetype)) {
whereSQL += " AND " + Data.MIMETYPE + " = ? ";
ids.add(mimetype);
}
whereArgs = ids.toArray(new String[ids.size()]);
Cursor cursor = context.getContentResolver().query(
Data.CONTENT_URI,
projection,
whereSQL,
whereArgs,
Data.RAW_CONTACT_ID);
return cursor;
I just wonder what will happen when the number of raw_contacts becomes large enough. I'm guessing that having a thousand or more raw_contact id's will break stuff.
I am creating a contacts application for android, I have successfully created a list view of the contacts and also retrieved the contact images(for those contacts which has images) and the contacts which doesn't have any image shows without any image is displayed without any image. I need the contacts without any images to be displayed with the default contact silhouette image. Here is the code
private void populateContactList() {
// TODO Auto-generated method stub
// Build adapter with contact entries
Cursor cursor = getContacts();
String[] fields = new String[] { ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.PHOTO_THUMBNAIL_URI };
adapter = new SimpleCursorAdapter(this, R.layout.contactentrylayout,
cursor, fields, new int[] { R.id.contactEntryText,
R.id.contactimageentry });
mContactList.setAdapter(adapter);
}
#SuppressWarnings("deprecation")
private Cursor getContacts() {
// TODO Auto-generated method stub
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Data.PHOTO_THUMBNAIL_URI };
// String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP +
// " = '"
// + (mShowInvisible ? "0" : "1") + "'";
String selection = ContactsContract.Contacts.DISPLAY_NAME
+ " LIKE ? OR "
+ ContactsContract.Contacts.DISPLAY_NAME_ALTERNATIVE
+ " LIKE ?";
String[] selectionArgs = { etquery.getText().toString() + "%",
etquery.getText().toString() + "%" };
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs,
sortOrder);
}
Hi i am doing a app on contact app i got the contact id and the Display name using the below code now what i want is the company name alone with the contact id and the display name.how can i get the Company name?
private void populateContactList() {
// Build adapter with contact entries
Cursor cursor = getContacts();
fields = new String[] { ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Contacts._ID};
contactqueryAdaptor = new Contactquery_adaptor(this, R.layout.row,
cursor, fields, new int[] { R.id.applese });
}
//getContacts()
private Cursor getContacts() {
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ (mShowInvisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs,
sortOrder);
}
According to this: Get company name form Content Provider in android using new API's, you'll need to query the ContactsContract.Data and use these aliases http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Organization.html
Friends I want Contacts which have email and also sort in ascending order..
any one know how to get this list and sort..
Please help me and thanks in advance.
I am using this code.
MatrixCursor matCur = new MatrixCursor(new String[] { Contacts._ID,
Contacts.DISPLAY_NAME, "photo_id", "starred" });
Cursor cEmail = WP7Main.this.managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
cEmail.moveToFirst();
if (cEmail.moveToFirst())
{
// String name =
// cursor.getString(cursor.getColumnIndexOrThrow(People.NAME));
String contactId = cEmail.getString(cEmail.getColumnIndex(ContactsContract.Contacts._ID));
Cursor emails = WP7Main.this.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + contactId, null, null);
String emailAddress = "";
while (emails.moveToNext())
{
// This would allow you get several email addresses
if (emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)) != null)
{
String[] columnValues = {
cEmail.getString(cEmail
.getColumnIndex("_id")),
cEmail.getString(cEmail
.getColumnIndex("display_name")),
cEmail.getString(cEmail
.getColumnIndex("photo_id")),
cEmail.getString(cEmail
.getColumnIndex("starred")) };
matCur.addRow(columnValues);
}
}
emails.close();
}
Try this:
/**
* #return A managed cursor of email contacts for the given activity.
*/
public static Cursor buildFilteredEmailCursor(Activity activity) {
final String my_sort_order = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
String my_selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
String[] eproj = new String[]{
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA};
Uri uri = android.provider.ContactsContract.CommonDataKinds.Email.CONTENT_URI;
return activity.managedQuery(uri, eproj, my_selection, null, my_sort_order);
}
Use this query :
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[]{Data.CONTACT_ID, Data.DISPLAY_NAME, Email.ADDRESS},
Data.MIMETYPE + "=?", new String[] {Email.CONTENT_TYPE}, Data.DISPLAY_NAME /* use Email.ADDRESS if you want to sort it using that*/);