2017-09-06 25 views
0

画像ここ</p> <p><a href="https://i.stack.imgur.com/jzYXW.png" rel="nofollow noreferrer">MG8ou.png</a></p> <p>私のコードに見られるように、私は、それが複数の番号を持っている場合の接触から電話を分離することができますどのように

@Override 
    protected Void doInBackground(Void... params) { 
     ContentResolver contentResolver = getActivity().getContentResolver(); 

     Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 

     if (cursor.getCount() > 0) { 
      while (cursor.moveToNext()) { 

       int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); 

       if (hasPhoneNumber > 0) { 
        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 

        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

        ContactObjets contactObjets = new ContactObjets(); 

        contactObjets.setContactName(name); 

        Cursor phoneCursor = contentResolver.query(
          ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
          null, 
          ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", 
          new String[]{id}, 
          null); 

        if (phoneCursor.moveToNext()) { 

         String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
         contactObjets.setContactNumber(phoneNumber); 
        } 
        phoneCursor.close(); 

        Cursor emailCursor = contentResolver.query(
          ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
          null, 
          ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
          new String[]{id}, null); 


        while (emailCursor.moveToNext()) { 
         String emailId = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
         contactObjets.setContactEmail(emailId); 
        } 
        emailCursor.close(); 

        contactObjetsList.add(contactObjets); 
       } 
      } 

     } 
     return null; 
    } 

私はAsyncTaskの中にこれを持っていますが、これはダイアログ内に表示されますが、最初の番号だけを呼び出しますが、複数の番号がある場合は連絡先がそれをしません。

どのように私は私の問題を解決することができ

答えて

0
 @Override 
    protected Void doInBackground(Void... params) { 
     ContentResolver contentResolver = getActivity().getContentResolver(); 

     Cursor cursor = contentResolver.query(
       Contacts.CONTENT_URI, 
       null, 
       null, 
       null, 
       Phone.DISPLAY_NAME + " ASC"); 

      while (cursor.moveToNext()) { 
        String id = cursor.getString(cursor.getColumnIndex(Contacts._ID)); 
        String name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME)); 

        Cursor phoneCursor = contentResolver.query(
          Phone.CONTENT_URI, 
          null, 
          Phone.CONTACT_ID + " = ?", 
          new String[]{id}, 
          null); 

        if (phoneCursor.moveToNext()) { 
         for (int i = 0; phoneCursor.getCount() > i; i++){ 
          phoneCursor.moveToPosition(i); 
          String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(Phone.NUMBER)); 
          int typePhone = phoneCursor.getInt(phoneCursor.getColumnIndex(Phone.TYPE)); 
          String type = ""; 
          switch (typePhone){ 
           case ContactsContract.CommonDataKinds.Phone.TYPE_HOME: 
            type = "Casa"; 
            break; 
           case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE: 
            type = "Celular"; 
            break; 
           case ContactsContract.CommonDataKinds.Phone.TYPE_WORK: 
            type = "Trabajo"; 
            break; 
           default: 
            type = "Otro"; 
            break; 
          } 
          ContactObjets contactObjets = new ContactObjets(); 
          contactObjets.setContactName(name); 
          contactObjets.setContactNumber(phoneNumber); 
          contactObjets.setContactType(type); 
          contactObjetsList.add(contactObjets); 
         } 
        } 
        phoneCursor.close(); 
      } 
     return null; 
    } 
関連する問題

 関連する問題