2016-04-24 45 views
0

我正在尝试在一个项目中工作,我需要其电话号码中有123个电话的所有联系人。我能够检索联系人,但“其中”在ContentResolver的条款是不是在这里工作是我的参考从android检索联系人,只有电话号码中包含123的联系人

代码
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) 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) (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) 引起:android.database.sqlite.SQLiteException:no such column:phone.NUMBER(code 1): ,

回答

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

感谢回答,使用此线的应用程序没有启动,并抛出错误的logcat –

+0

你能后的错误日志? –

+0

您应该使用Phone.NUMBER而不是NUMBER。更新了答案。 –

相关问题