【问题标题】:Retrieve contacts from android only where phone number consists 123 in it仅从 android 中检索电话号码包含 123 的联系人
【发布时间】:2016-08-17 12:19:57
【问题描述】:

我正在尝试一个项目,我需要电话号码中包含 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) 在 java.lang.reflect.Method.invoke(本机方法) 在 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):,

【问题讨论】:

    标签: android android-sqlite contactscontract


    【解决方案1】:

    您需要更改您的查询 -

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

    【讨论】:

    • 感谢您的回复,使用此行应用程序未启动,并且在 logcat 中引发错误
    • 您应该使用 Phone.NUMBER 而不是 NUMBER。更新了答案。
    • 我已经用 logcat 错误更新了我的问题,请帮忙
    • 您使用了“phone.NUMBER”。该列应为“Phone.NUMBER”。
    • 仍然几乎相同的位置没有这样的列 Phone.NUMBER(code1)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多