2014-04-02 50 views
0

我想在contactlist 匹配一些我在我的活動我試圖contactlist與存儲在contactlist一些匹配,但我沒有得到的結果爲每.... 的代碼contactlist那我用的是...我該如何將號碼與存儲在聯繫人列表中的號碼進行匹配?

String phoneNumber = 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; 


      String DATA = ContactsContract.CommonDataKinds.Email.DATA; 

      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, Phone_CONTACT_ID + " = ?", 
           new String[] { contact_id }, null); 

         while (phoneCursor.moveToNext()) { 
          Toast.makeText(getApplicationContext(), number, Toast.LENGTH_SHORT).show(); 
//i am getting the incoming call number here, the toast is displayed 
     //String number="+919729226730";      
          phoneNumber = phoneCursor.getString(phoneCursor 
          .getColumnIndex(NUMBER)); 
        if(number.equals(phoneNumber)){ 
         Toast.makeText(getApplicationContext(), "got it", Toast.LENGTH_SHORT).show(); 
         Log.i("phone", "number matched"); 
        } 



        output.append("\n Phone number:" + phoneNumber); 
         } 

         phoneCursor.close(); 




        } 

        output.append("\n"); 

       } 

回答

0

更改順序

改變這一點:

if(number.equals(phoneNumber)){ 
    Toast.makeText(getApplicationContext(), "got it", Toast.LENGTH_SHORT).show(); 
     Log.i("phone", "number matched"); 
} 


phoneNumber = phoneCursor.getString(phoneCursor 
      .getColumnIndex(NUMBER)); 

phoneNumber = phoneCursor.getString(phoneCursor 
          .getColumnIndex(NUMBER)); 

if(number.equals(phoneNumber)){ 
    Toast.makeText(getApplicationContext(), "got it", Toast.LENGTH_SHORT).show(); 
    Log.i("phone", "number matched"); 
} 

獲取phoneNumber,然後再進行比較。

+0

但其在來電的情況下不工作...我得到了來電號碼,我保存的數量可變的,但我沒有得到那個麪包......你能告訴先生什麼背後的原因。 .. ???? –

+0

我已經更新了上面的代碼... –

+0

Log.e你得到的數字可能是數字有 - 或者空間,因此它不匹配數據庫中的數據?代碼中的哪個位置初始化了數字? –

相關問題