Pratik Modak
Pratik Modak

Reputation: 11

Retrieve contacts from android only where phone number consists 123 in it

I am trying to work on a project where I require all contacts of a phone which has 123 in its phone number. I am able to retrieve the contacts but "where" clause in contentresolver isn't working here's my code for reference

public void fetchContacts() {

        String phoneNumber = null;
        String email = null;

        Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
        String _ID = ContactsContract.Contacts._ID;
        String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
        String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;

        Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
        String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;



        StringBuffer output = new StringBuffer();

        ContentResolver contentResolver = getContentResolver();

        Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null);

        // Loop for every contact in the phone
        if (cursor.getCount() > 0) {

            while (cursor.moveToNext()) {

                String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
                String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));

                int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));

                if (hasPhoneNumber > 0) {

                    output.append("\n First Name:" + name);

                    // Query and loop for every phone number of the contact
                    Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null,  NUMBER + " = 123", null, null);

                    while (phoneCursor.moveToNext())

                    {

                        phoneNumber = cursor.getString(phoneCursor.getColumnIndex(NUMBER));
//
                        output.append("\n Phone number:" + phoneNumber);


                    }



                    phoneCursor.close();



                }

                output.append("\n");
            }

            outputText.setText(output);
        }
    }


}

LOGCAT java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gamemyworldgame.contact/com.gamemyworldgame.contact.MainActivity}: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5257) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: android.database.sqlite.SQLiteException: no such column: phone.NUMBER (code 1): ,

Upvotes: 0

Views: 168

Answers (1)

Shadab Ansari
Shadab Ansari

Reputation: 7070

You need to change your query -

Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,  "ContactsContract.CommonDataKinds.Phone.NUMBER like '%123%'", null, null);

This will fetch you all those phone number which have '123' anywhere in that number.

Another way -

final String keyword = "%123%"; // contains an "123"

Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,  ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE ?", new String[] { keyword }, null);

Upvotes: 0

Related Questions