how to get phone numbers for particular group - android

I'm working on Android contact. I want to query phone numbers (not contact name) from a specific group name. What query should i perform in order to do this?

Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI
, null, ContactsContract.Data.MIMETYPE+"=?"
, new String[]{ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE}
, null);
Then loop through cursor and get data u want. This will return data blocks showing the contactID and the groupID and other info. With this then query ContactsContract.Groups and get data about the group to compare.
If you are looking for specific data about a group first query for group row ID than you can add that to the following cursor like so...
Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI
, null, ContactsContract.Data.MIMETYPE+"=? AND "+ ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID+"=?"
, new String[]{ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE, rowID }
, null);
wrote code here so sorry for silly mistakes
You can find group id like so...
Cursor c = context.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, new String[]{ContactsContract.Groups._ID}, ContactsContract.Groups.TITLE+"=?","myGroup", null);

Related

Getting All data from specific column in SQLITE Android

There is android sqlite database with one table, im using this query for getting values :
Cursor cursor = db.query(Table_Items, null, "type=? AND operationtype=? AND problemtype=?",
new String[] { roosazi,type,problem }, null, null,KEY_Items_ID+" "+date , null);
everything is working fine.
question is:
how can i get all from specific column? let me give an example:
all values with type="A" and operationtype="XYZ" are needed , no matter what problemtype is! of course i can use something like this :
Cursor cursor = db.query(Table_Items, null, "type=? AND operationtype=?",
new String[] { roosazi,type }, null, null,KEY_Items_ID+" "+date , null);
but problem is sometimes problemtype is X and sometimes its like ALL!
how can i achieve this? can i put something like * instead of problem?
thank u so much
I understand that you want to use the same query to get sometimes with a particular problem type like X,Y or Z, and sometimes with any problem type,
If that is the case you could use the statement 'like' in your query instead of '=' in the problemtype field
Cursor cursor = db.query(Table_Items, null, "type=? AND operationtype=? AND problemtype like '?'",
When you want to return all values just past
problem = "%"
% simbol means any characters,
When you want to return values with a particular problem use
problem = "X"
in the values array
new String[] { roosazi,type,problem }

Cannot get contact details from some contact

I've pulled out most of my hair now and really needs some help
before I go completely bold
I'm trying to launch an action picker to select a Contact that has a phone number.
When that contact is selected, I want to extract the name and phone number.
But this only happens for some contacts, not all.
The code is rougly as follows:
Select contact:
Intent contactPicker = new Intent(Intent.ACTION_PICK, contactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPicker, REQ_PICK_CONTACT);
Extract id (notice getData().getLastPathSegment():
onContactForImportPicked(intent.getData().getLastPathSegment());
and then I try to fetch this contact:
String[] fields = new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY
};
Cursor cursor = content.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
fields,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[] { id }, // SEE BELOW
null);
It this point, many contacts are fetched correctly, but a lot is
also non-existing. cursor.getCount() == 0. In the 'SEE BELOW'
section above, I've tried various other fields, linke
Contact._ID, Phone._ID etc etc.
Any idea why some contacts are not fetched with this method?
When I use your code, I don't even get the contact I selected correctly. The intent data contains the URI to the data you want, so you can use that directly.
Try --
Cursor cursor = content.query(data.getData(),
fields,
null,
null,
null);

Android- Fetch the contacts based on contact id greater than some value

i want to fetch the contacts from android periodically. for that i
want to something like this. i am taking all the conatcts from the
android and store it in my local db. so i will store last contact id.
so next time on wards if any contacts added , i need to get those
contacts only so for that purpose i want to fetch the contact id
greater than last time fetched contact id. how can write the query for
content resolver to fetch the contacts some thing like.
Select * from tablename where id > 100;
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
null, // this will be the columns selection
ContactsContract.Contacts._ID + ">100", // this is where you add the "where" part of the query, id is 1 in this case
null, // where args, you are skipping these
null); // sort order
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
long id = Long.parseLong(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)));
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)).trim();
}
}
Hope this solves your issue

getting notes from a single contact - android

I'm trying to get "notes" from a single contact. It added fine but retrieving it has been a problem.
String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" like'%" + sender +"%'";
String[] projection = new String[] { ContactsContract.CommonDataKinds.Note.NOTE};
Cursor c2 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, selection, null, null);
if (c2.moveToFirst()) {
notes = c2.getString(0);
}
It works fine with other values like name or phone number but can't seem to get notes to retrieve correctly. It retrieves a random value like email instead.
I believe that your problem is that not all rows in the table represent contact types that have notes. You have to request the proper MIME Type.
ContactsContract.CommonDataKinds.Note is an alias for the 'data1' column that is present on all rows, so when you get a row of a different MIME Type, it represents different data.
How to get contacts in Android should give you an idea of how to do this.

get contacts those are not related to any group

I am working on the displaying the contacts group wise in android application. So I got all the group contacts from the particular group but now I want to get the contacts those are not in any of the group (NOT ASSIGNED).
So what can be the value of GROUP_ROW_ID in the contacts's in case of no group ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID ?
If there any solution you know please let me know.
Any suggestion will be appreciated.
Please use,
ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID = null;
This will get non-group contacts.
you can retrieve all contacts as a cursor.
Cursor cursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
now we have cusror with contacts and get diffrent value from cusror.
while (cursor.moveToNext()) {
String name =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
see this one http://samir-mangroliya.blogspot.in/p/android-read-contact-and-display-in.html

Categories

Resources