0

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

Community
  • 1
  • 1
siva
  • 167
  • 1
  • 12
  • i am getting the following error by second method . Bad request for field slot 1,-1. numRows = 2, numColumns = 23, give me some fix for this problem – siva Feb 29 '12 at 09:47

2 Answers2

0

In your second code snippet,change these lines:

...
    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
    {
        if(Name.equalsIgnoreCase(name))
        {
             Cursor tempc=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,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)); 
        }
   }
   else
         continue;
...

EDIT :

As you told in your question that your are thrown error for this line:

Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                "DISPLAY_NAME = ?", args, null);    

then change this line to below one and try:

Cursor cursor=cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" like '"+args+"'", null,null);

EDIT - 1 :

Cursor tempc=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)), null,null);
Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
  • your way is not giving the correct results, but i have accepted your answer. i will post my answer now. but ur help is valuable to me,it is by ur motivation i have worked it out... – siva Mar 01 '12 at 06:20
  • @siva: thats so nice of you.But if my answer didn't work for you completely,please don't accept it.It will mislead others who have got the same problem as you.So if you already found a correct and complete answer to your question,please post your answer and accept it.That will help others too. :) – Hiral Vadodaria Mar 01 '12 at 06:44
0
void get_Number(String name) 
{       
        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);
            }
        }    
        if (number != null)
            txt.append(number + "\n");
        else
            txt.append("No name");    
  }

or else

String where= "DISPLAY_NAME like ?";            
Cursor  people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,    
 null, where, new String[]{name}, null);

people.moveToFirst();  

try{
        String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
        String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
        if ( hasPhone.equalsIgnoreCase("1"))
              hasPhone = "true";
        else
              hasPhone = "false" ;
        if (Boolean.parseBoolean(hasPhone)) 
        {
              Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
              while (phones.moveToNext()) 
              {
                   number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                   // mConno.add(position,phoneNumber);    
              }
              phones.close(); 
        }               
}       
catch(Exception e){ 
}

if(number!=null)
     txt.append(number + "\n");
else
     txt.append("No name");

The reason why it is not working for multi word strings is the entire name must be saved as first name, dont split it into first name and last name.then anyone of above approaches would work for u!

Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
siva
  • 167
  • 1
  • 12