仅在电话号码为123的情况下从Android中检索联系人

时间:2016-04-24 00:41:24

标签: android android-sqlite contactscontract

我正在努力开展一个项目,我需要手机的所有联系人,其电话号码为123。我能够检索联系人,但contentresolver中的“where”子句在这里不起作用我的代码供参考

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:无法启动活动ComponentInfo {com.gamemyworldgame.contact/com.gamemyworldgame.contact.MainActivity}:                                                                                在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)                                                                                在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)                                                                                在android.app.ActivityThread.access $ 800(ActivityThread.java:151)                                                                                在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)                                                                                在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                在android.os.Looper.loop(Looper.java:135)                                                                                在android.app.ActivityThread.main(ActivityThread.java:5257)                                                                                at java.lang.reflect.Method.invoke(Native Method)                                                                                在java.lang.reflect.Method.invoke(Method.java:372)                                                                                在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)                                                                                在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)                                                                             引起:android.database.sqlite.SQLiteException:没有这样的列:phone.NUMBER(代码1):,

1 个答案:

答案 0 :(得分:0)

您需要更改查询 -

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

这将获取所有那些在该号码的任何位置都有“123”的电话号码。

另一种方式 -

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);