2016-06-22 13 views
0

Ich benutze Kontaktauswahlbibliothek für die Auswahl mehrerer Kontakte, aber wenn ein Kontakt keine Zahl enthält und wenn es ausgewählt ist, zeigt es einige Nullzeiger Ausnahme in der Bearbeitung Textfeld. Wie Sie diese Nachricht entfernen und auch das nachgestellte Komma entfernen können. Unten ist mein Code.So zeigen Sie Nur wenn Telefonnummer mit Kontaktpicker vorhanden ist

try { 
      int pos = 0; 
      for (Contact contact : contacts) { 
       String displayName = contact.getDisplayName(); 
       result.append(displayName + ","); 
       result.setSpan(new BulletSpan(15), pos, pos + displayName.length() + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 
       //pos += displayName.length() + 1; 
      } 
     } 
     catch (Exception e) { 
      result.append(e.getMessage()); 
     } 

     contactsView.setText(result); 
+0

Welche Bibliothek verwenden Sie? Hat Contact-Klasse eine Methode, um PhoneNumber zu bekommen? – Pr38y

+0

com.onegravity.contactpicker diese Bibliothek ........ ContactPickerActivity ist die Klasse und ja, es hat eine Methode getPhoneNumber – stackover65

+0

dann überprüfen, ob die Bedingung zu überprüfen Telefonnummer ist da oder nicht, basierend auf diesem Update "Ergebnis" – Pr38y

Antwort

0

bitte versuchen Sie es diesem Code

void getAllContacts() { 
    ArrayList<String> nameList = new ArrayList<>(); 
    ArrayList<String> numberList = new ArrayList<>(); 

    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
    String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER; 
    String[] list = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.Contacts._ID}; 
    Cursor cursor = getContentResolver().query(uri, list, selection, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 

    cursor.moveToFirst(); 
    if (cursor.moveToFirst()) { 
     do { 
      String contactNumber =  cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 


      contactNuber.add(contactNumber); 
      contactsName.add(contactName); 
      nameList.add(contactName); 
      numberList.add(contactNumber); 

     } while (cursor.moveToNext()); 
     cursor.close(); 
     myContacts.put("name", nameList); 
     myContacts.put("number", numberList); 


    } 

} 
0

Zum ContactFragment und ersetzen onEventMainThread() mit dem unter einem ....

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN) 
    public void onEventMainThread(ContactsLoaded event) { 
     EventBus.getDefault().removeStickyEvent(event); 

     List<Contact> c = new ArrayList<>(); 

     mContacts = event.getContacts(); 
     // mFilteredContacts = mContacts; 
     for (Contact contact : mContacts) { 
      if (contact.getPhone(2) != null) 
       c.add(contact); 

     } 
     mFilteredContacts = c; 
     mContacts = c; 
     mAdapter.setData(mFilteredContacts); 

     updateEmptyViewVisibility(mContacts); 
    } 

Hoffnung zu überprüfen, dies wird Hilfe .....

Verwandte Themen