I want to retrieve the contact details along with the group which it belongs to. I got the code to list all the contact groups in the phone.
Cursor groupC = getContentResolver().query(
ContactsContract.Groups.CONTENT_URI, null, null, null, null);
while (groupC.moveToNext()) {
String groupid =
groupC.getString(groupC.getColumnIndex(ContactsContract.Groups._ID));
Log.e("myTag", groupid);
String grouptitle =
groupC .getString(groupC.getColumnIndex(ContactsContract.Groups.TITLE));
Log.e("myTag", grouptitle);
}
groupC.close();
Then I tried to query for a particular contact by using its id but it always shows There is no such column....
Cursor groupC = getContentResolver().query(
ContactsContract.Groups.CONTENT_URI,
null,
ContactsContract.Contacts._ID+"= ?",
new String[]{id},
null);
where id is
Cursor cur = cr.query(
ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
null);
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
How to query the group using a particular contact id?
I found the answer.we should pass the raw contact-id and the correct mime type.
String where = ContactsContract.Data.RAW_CONTACT_ID
+ "="
+ Integer.parseInt(id)
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
+ "'";
Cursor cursor = ctx
.getContentResolver()
.query(ContactsContract.Data.CONTENT_URI, null, where, null,
null);
startManagingCursor(cursor);
Log.e("Count is:", ""+ cursor.getCount());
while (cursor.moveToNext()) {
groupid = cursor
.getString(cursor.getColumnIndex(ContactsContract.Data.DATA1));
Log.e("groupid", groupid);
builder.append(groupid);
}String where = ContactsContract.Data.RAW_CONTACT_ID
+ "="
+ Integer.parseInt(id)
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE
+ "'";
Cursor cursor = ctx
.getContentResolver()
.query(ContactsContract.Data.CONTENT_URI, null, where, null,
null);
startManagingCursor(cursor);
Log.e("Count is:", ""+ cursor.getCount());
while (cursor.moveToNext()) {
groupid = cursor
.getString(cursor.getColumnIndex(ContactsContract.Data.DATA1));
Log.e("groupid", groupid);
break;
}
A contact may be in more than one group,here it retrivr its first group pnly.
I think this may be useful to somebody...
Related
I already saw other similar questions and none worked.
With the code below I was able to get the default phone number of a contact but not all of them.
Cursor contact = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
contact.moveToFirst();
phoneNumberList.add(contact.getString(contact.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
Then I tried to create multiple cursors with more details about the phone type and then add them to a list:
Cursor contact = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " + ContactsContract.CommonDataKinds.Phone.TYPE + " = " + ContactsContract.CommonDataKinds.Phone.TYPE_HOME, new String[]{contactID}, null);
But unfortunately, it didn't solve anything. Would be thankful if someone helps me with this problem.
I'm assuming you want to get all phone numbers with their type (home, mobile, main, etc.).
Here's how to do it (the list of phones will be printed to log=:
String projection = new String[] { Phone.NUMBER, Phone.TYPE, Phone.LABEL };
String selection = Phone.CONTACT_ID + "=" + contactId;
Cursor cur = getContentResolver().query(Phone.CONTENT_URI, projection, selection, null, null);
while (cur != null && cur.moveToNext()) {
String number = cur.getString(0);
int type = cur.getInt(1); // home / office / personal
String label = cur.getString(2); // a custom label in case type is "TYPE_CUSTOM"
String labelStr = Phone.getTypeLabel(getResources(), type, label);
Log.d(TAG, "got a phone: " + number + " (" + labelStr + ")");
}
if (cur != null) {
cur.close();
}
DEBUG VERSION
String projection = new String[] { Phone._ID, Phone.CONTACT_ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL };
String selection = Phone.CONTACT_ID + "=" + contactId;
Cursor cur = getContentResolver().query(Phone.CONTENT_URI, projection, selection, null, null);
if (cur != null) {
DatabaseUtils.dumpCursor(cur);
cur.close();
}
I am using the following code and I am getting multiple names and phone number for the same person. How can I get single name and only mobile numbers of all users whose number I have dialed.
Code:
if (android.os.Build.VERSION.SDK_INT >= 21) {
mCursor = managedQuery(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Contacts.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'", null, ContactsContract.Data.CONTACT_LAST_UPDATED_TIMESTAMP + " DESC");
}else{
mCursor = managedQuery(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Contacts.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'", null, ContactsContract.Data.LAST_TIME_CONTACTED + " DESC");
}
int number = mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int name = mCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
while (mCursor.moveToNext()) {
String phName = mCursor.getString(name);
String phNumber = mCursor.getString(number);
}
}
updated code:
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));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.Data._ID, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.LABEL},
ContactsContract.Data.CONTACT_ID + "=?" + " AND "
+ ContactsContract.Data.MIMETYPE + "= + Phone.CONTENT_ITEM_TYPE + ",
new String[]{String.valueOf(id)}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.w("num", "Name: " + name + ", Phone No: " + phoneNo);
}
pCur.close();
}
}
}
Instead of manageQuery, you should use following with specific filed which you want to keep in your SELECT query. Below is just example, you can refer and use as per your requirement:
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
Data.CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "= + Phone.CONTENT_ITEM_TYPE + ",
new String[] {String.valueOf(contactId)}, null);
This is my table:
private static final String CREATE_TABLE_EMPLOYEES = "CREATE TABLE "+ TABLENAME + "(" +
COLUMNS[0] + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , " +
COLUMNS[1] + " TEXT NOT NULL , " +
COLUMNS[2] + " TEXT NOT NULL , " +
COLUMNS[3] + " TEXT NOT NULL , " +
COLUMNS[4] + " TEXT NOT NULL , " +
COLUMNS[5] + " TEXT NOT NULL " +
");";
And query all data from database:
public List<Employee> getEmployees() {
List<Employee> employees = new ArrayList<Employee>();
Cursor cur = db.query(dbHelper.TABLENAME, columns, null, null, null, null, null);
cur.moveToFirst(); // need to start the cursor first...!
while(!cur.isAfterLast()) { // while not end of data stored in table...
Employee emp = new Employee();
emp.setId(cur.getInt(0));
emp.setName(cur.getString(1));
emp.setCharge(cur.getString(2));
emp.setDepartament(cur.getString(3));
emp.setPhone(cur.getString(4));
emp.setEmail(cur.getString(5));
employees.add(emp);
cur.moveToNext(); // next loop
}
cur.close(); // !important
return employees;
}
I want to query all data if employee name =="ali"
Please help me.
I want to query all data if employee name =="ali".
3rd and 4th parameter is available in query method for adding WHERE clause in query.
Do it as:
Cursor cur = db.query(dbHelper.TABLENAME, columns,
"name=?",
new String[] { "ali" },
null, null, null);
Try this, this will also help you prevent from sql injection
Cursor cur = db.query(dbHelper.TABLENAME, columns, columns[1]+" = ?", new String[]{"ali"}, null, null, null);
Replace
Cursor cur = db.query(dbHelper.TABLENAME, columns, null, null, null, null, null);
with
Cursor cur = db.query(dbHelper.TABLENAME, columns, columns[1]+" = ?", new String[]{"ali"}, null, null, null);
The 3rd parameter in db.query() method is "selection statement"
and the 4th parameter is "selection arguments".
Use this cursor to query all data if employee name =="ali":-
String selection = COLUMNS[x] + " = " + "'" + ali + "'";
Cursor cur = db.query(dbHelper.TABLENAME, columns, selection, null, null, null, null);
Here COLUMNS[x] should be the column containing the employee names and "x" be the respective column number.
This cursor will fetch you only the records/tuples for the employee named "ali".
Hi I am working in Android Contact search module.I am running below Query.
cur = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null , null ,null, null);
from this query I am getting Result Multiple times.Is there any thing which I am doing wrong.I want DISTINCT Result Set.
please help me.
I think you mean you got duplicate record for some contacts. So you must add condition for your query
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ ("1") + "'";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
cur = context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, projection, selection
+ " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
+ "=1", null, sortOrder);// this query only return contacts which had phone number and not duplicated
Try this code will help you
public void getContact() {
Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
ContentResolver contect_resolver = getContentResolver();
int size = cur.getCount();
if (size > 0 && cur != null) {
for (int i = 0; i < size; i++) {
cur.moveToPosition(i);
String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String name = "";
Cursor phoneCur = contect_resolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
if (phoneCur.moveToFirst()) {
name = phoneCur.getString(phoneCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if (!name.equalsIgnoreCase("")) {
String id1 = phoneCur.getString(phoneCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Cursor emails = getContentResolver()
.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = " + Integer.parseInt(id1),
null, null);
emailAddress="";
if (emails!=null && emails.getCount() > 0) {
emails.moveToFirst();
emailAddress = emails
.getString(emails
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
}
emails.close();
contact.setEmail(emailAddress);
id1 = "";
mcontact_arraylist.add(contact);
}
phoneCur.close();
}
}
cur.close();
}
}
Each record should contain a portion of the data for a contact (eg each phone number or address is a separate row) each row has a mimetype associated with it that is used to determine the data stored in each column. So for an address, the "data1" column holds the street data and data4 might hold the state.
I need to set the cursor to a specific contact that i have its id
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null,null, null, null);
I have that code for all the database, how can I optimize it?
I need the cursor to return the name and the default number of the contact
thank you
You actually need two cursors. One for name & id and another one for phone number.
Cursor c = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (c.moveToFirst()) {
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
fullName = c.getString(c.getColumnIndex("display_name_alt"));
id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(c.getString(c.getColumnIndex
(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//phone number
Cursor phones = getContentResolver().query(Phone.CONTENT_URI,
null,
Phone.CONTACT_ID + " = " + id,
null,
null);
while (phones.moveToNext()) {
String number_type = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.TYPE));
if(number_type.equalsIgnoreCase("1"))
number1 = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER));
if(number_type.equalsIgnoreCase("2"))
number2 = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
}
c.close();
cursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] { id },
Phone.DISPLAY_NAME + " ASC");