2016-04-24 46 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。更新了答案。 –

相關問題