i have tried the links this one
but still i am getting errors and working weirdly. i am posting my code, so please help me. i am confused about the parameters to pass in the query.
i am using the following method
void get_Number(String name)
{
//String name = "Daddy";
String number = null;
String args[]= {name};
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = ?", args, null);
if (cursor.moveToFirst()) {
String contactId = cursor.getString(cursor
.getColumnIndex(BaseColumns._ID));
Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID
+ " = " + contactId, null, null);
while (phones.moveToNext()) {
number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
Log.v("TAG3", number);
}
}
the problem is in the line
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, "DISPLAY_NAME = ?", args, null);
The above method is working for single word strings, but not for multi word strings . how can i modify it to work correctly for multi word names in contacts.
the otherway i have tried is
try
{
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
//String iur.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)
{
if(Name.equalsIgnoreCase(name))
{
Cursor tempc=
etContentResolver().query(ContactsContract.CommonDataKinds.
Phone.CONTENT_URI,new String[]
{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone._ID+
"="+cur.getString(cur.getColumnIndex
(ContactsContract.Contacts._ID)), null,null);
tempc.moveToFirst();
//retrieve the phone number
number =
tempc.getString(cur.getColumnIndex
(ContactsContract.PhoneLookup.NUMBER));
}
}
}
}
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
if(number!=null)
txt.append(number + "\n");
else
txt.append("No name");
the problem is with the code nested in if(Name.equalsIgnoreCase(name))
clause.
i have tried various ways to retrieve the number from the cursor variable, but it is throwing me an error. so please show me the correct way to retrieve the number if there is a name match for contact. thanks in advance.
Somebody please help i am trying it for past 2 days...
and this is the output of the logcat
02-29 16:56:04.458: E/CursorWindow(1197): Bad request for field slot 0,-1. numRows = 1, numColumns = 1