Android SDK-Get specific contacts from contact list - java

I'm trying to query the contacts table to get all the contacts of a specific name, for example all the contacts with first name "George"
At the moment I get all the contacts with this
Cursor contactsCursor = managedQuery(contactData,
null, null, null, null);
Any help is appriciated :)

Cursor cursor = managedQuery(ContactsContract.Data.CONTENT_URI, null,
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME + " = ?",
new String[] { "George" }, null);

Related

How to get all contacts in Android and ignore email-only contacts

Below is the code I am using to fetch contacts in Android.
String[] projectionFields = new String[]{
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
};
CursorLoader cursorLoader = new CursorLoader(context,
ContactsContract.Contacts.CONTENT_URI,
projectionFields, // the columns to retrieve
SELECTION, // the selection criteria (none)
null, // the selection args (none)
null // the sort order (default)
);
Cursor cursor = cursorLoader.loadInBackground();
if (cursor.moveToFirst()) {
int nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
do {
String contactDisplayName = cursor.getString(nameIndex);
} while (cursor.moveToNext());
}
I am also getting a lot of email only contacts. I don't want to show email-only contacts. How can I go about this?
I understand from your question that you need to get the contacts that has phone numbers. If this is the case, you can check if the contact has phone number by
ContentResolver contentResolver = getContentResolver();
ContentProviderClient mCProviderClient = contentResolver.acquireContentProviderClient(ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
Cursor cursor = mCProviderClient.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
Cursor phones = mCProviderClient.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION_PHONE,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
}
mCProviderClient.close();
cursor.close();
}
The value of hasPhoneNumber will be > 0, if there is a phone number assigned to the contact.
Please refer to this answer, it will allow only numbers and will also remove duplication.
Hope you find it helpful and your problem gets resolved.
Thanks.

Android ContactsProvider: How to get name and phone number in one row for specific contact?

I want to retrieve a cursor where in one row contains both name and phone number. Is the way to do this pretty, without any loops?
Uri uri = ContactsContract.Data.CONTENT_URI;
getContentResolver().query(uri,
new String[] {ContactsContract.Data._ID, Phone.NUMBER, StructuredName.GIVEN_NAME}, null, null, null)
the code i use to get my contacts is:
Cursor cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
String Contactos =" CONTACTOS DEL TELEFONO "+cursor.getCount()+"\n";
while (cursor.moveToNext()) {
String name =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contactos+="Nombre: "+name+"\n"+
"Telefono: "+phoneNumber+"\n"+
"----------------------------\n";
}
then add the Contactos to a textview, and that's all!
see ya!

Android retrieving phone contacts query takes long time

Hello i am using the next code for retrieving phone contacts name,number and its taked to long.
After littel search i found the the cursor.moveToFirst() command is that one who cuz this .
But i couldent find any solution ): may some one help me please ?
Thanks !
public void loadPhoneMembers(){
ContentResolver cr = this.getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if(cursor.moveToFirst())
{
do
{
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
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())
{
String contactNumber=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactName =
pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if(!phonesAdded.contains(contactNumber)){
members.add(contactName+","+contactNumber); // members is arrayList
}
break;
}
pCur.close();
}
} while (cursor.moveToNext()) ;
}
}
You are querying one extra Cursor for each Contact from your first query. You can query the Names and TelephoneNumbers in one query and ignore the Contacts without Telephone number.
String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER ,ContactsContract.CommonDataKinds.Phone.PHOTO_URI};
Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,projection, null, null, null);
Also, while (cursor.moveToNext()){
}
has same effect as the following.
if(cursor.moveToFirst())
{
do
{
} while (cursor.moveToNext()) ;

Android - How to get phone # from contacts

I want to get a phone number to dial from contacts , here is the code I'm testing:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (Integer.parseInt(cur.getString(
cur.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()) {
// Do something with phones
}
pCur.close();
}
For some reason it gets error at 'new Sting[]{id}, null);
Eclipse error #1 -id cannot be resolved to a variable
Eclipse error #2 -type mismatch: cannot convert from ArrayList to String
It appears maybe, as im a noob, 'id' is already being used by ArrayList ? But when i change 'id' to 'id2' Elcipse still dosent like it...
Please advise?
Thanks,
FlinxSYS
Here is working code for you:
public static void getContacts(ContentResolver cr) {
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
// read id
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
// read names
String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// Phone Numbers
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String typeStr = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
}
pCur.close();
...
Reference : http://ganeshtiwaridotcomdotnp.blogspot.com/2011/10/android-code-for-reading-phone-contacts.html

need help with androids sql query

I have an error in that m
String currentDateTimeString = DateFormat.getDateInstance().format(new Date());
Cursor myCursor=db.query(true, DATABASE_TABLE, new String[]{KEY_ROWID,MONEY_FIGURE,SPENDING_DETAILS,DATE}
, KEY_ROWID , null, null, DATE+"="+currentDateTimeString, KEY_ROWID, null);
The query function by the androids api:
query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
What I am trying to do here is to say. give me all the data based on where the date equals
currentDateTimeString .
But I get an error on that statement..
dont ask me what the error is..when i remove the call to the method holding that query statement the error doesnt appear..so i guess it is an sqlexception
SOLVED MY ME:
Cursor myCursor=db.query(true, DATABASE_TABLE, new String[]{KEY_ROWID,MONEY_FIGURE,SPENDING_DETAILS,DATE}
,DATE+"='"+currentDateTimeString+"'" , null, null, null , KEY_ROWID, null);
I am trying to do this
SELECT MONEY_FIGURE,SPENDING_DETAILS,DATE
FROM SpendingManagmentTable
WHERE Date=# currentDateTimeString
If the column type is not number then have to use with ''
date='1/1/2011' not date=1/1/2011
Cursor c = db.query(DATABASE_TABLE, new String[] {{KEY_ROWID,MONEY_FIGURE,SPENDING_DETAILS,DATE},DATE+"='"+currentDateTimeString+"'", null, null, null, null);

Categories