2017-01-12 1 views
0

Das Problem, das ich konfrontiert ist, kann ich nicht den abgerufenen Wert anzeigen, wie ich eine Listenansicht verwendet habe, um die Werte anzuzeigen nicht erreichen. Dies ist mein Code-Snippet, das mehrere Kontakte aus dem Telefonbuch holt. Hope any1 hilf mir, es zu lösen.holen mehrere Kontakte aus dem Telefonbuch und Display

private void chooseContact() { 

    Intent phonebookIntent = new Intent("intent.action.INTERACTION_TOPMENU"); 
    phonebookIntent.putExtra("additional", "phone-multi"); 
    phonebookIntent.putExtra("maxRecipientCount", MAX_PICK_CONTACT); 
    phonebookIntent.putExtra("FromMMS", true); 
    startActivityForResult(phonebookIntent, REQUEST_CODE_PICK_CONTACT); 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == Activity.RESULT_OK) { 
     if (requestCode == RESULT_PICK_CONTACT) { 

      Uri contractData = data.getData(); 

      ContactRetriever cr = new ContactRetriever(getApplicationContext(), contractData); 
      Person p = cr.getPerson(); 

      if (p == null) 

       Toast.makeText(this, "Phone number not found!", Toast.LENGTH_SHORT).show(); 

      else { 
       PersonManager.savePerson(p, getApplicationContext()); 
       listContacts.setAdapter(new PersonAdapter(this, PersonManager.getSavedPersons(this))); 
      } 
     } 
    } 
}} 

Und der Uri-Wert und

in nächsten unten Klasse verwendet genannt wird
public class ContactRetriever { 
    private final String TAG = "ContactRetriever"; 

    private ContentResolver cr; 
    private Context context; 

    private Uri contractData; 

    private String id; 



    public ContactRetriever(Context context,Uri contractData) { 
     this.context = context; 
     this.cr = context.getContentResolver(); 
     this.contractData = contractData; 
    } 

    public Person getPerson() { 
     String name = getName(); 
     String num = getNumber(); 

     if (name != null && num != null) 
      return new Person(getNumber(), getName()); 
     else return null; 
    } 



    private String getNumber() { 
     String ret = null; 

     Cursor cId = cr.query(contractData, new String[]{ContactsContract.Contacts._ID}, null, null, null); 

     if (cId.moveToFirst()){ 
      id = cId.getString(cId.getColumnIndex(ContactsContract.Contacts._ID)); 
      Log.i(TAG + " IDs: ", id); 
     } 
     cId.close(); 

     Cursor cNum = cr.query(contractData, null, null, null, null); 

     if (cNum.moveToNext()){ 
      ret = cNum.getString(cNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      Log.i(ret, TAG + " NUMBERS: "); 
     } 
     return ret; 
    } 

    private String getName() { 
     String ret = null; 

     Cursor c = cr.query(contractData, null, null, null, null); 
     if (c.moveToFirst()) 
      ret = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

     c.close(); 
     Log.i(TAG + "NAMES: ", ret); 
     return ret; 
    } 
} 
+0

Wenn i-Bundle Bundle verwenden = data.getExtra() um die geholten Daten zu bekommen ... es wirft immer noch einen Fehler in Uri ..... Das Problem ist, dass das erste Code-Snippet nicht korrekt mit der nächsten Klasse von Code verknüpft ist – Meghana

+0

Bitte platziere deinen Edit zurück in die Frage. –

+0

Ich habe immer noch nicht den Code – Meghana

Antwort

0

Hier ist Code meine Arbeits für mein

//enter code here 



private void getAllContacts() { 
     List<InviteContactInfo> inviteContactList = new ArrayList(); 
     InviteContactInfo inviteContactInfo; 

     ContentResolver contentResolver = 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)); 

        inviteContactInfo = new InviteContactInfo(); 
        inviteContactInfo.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)); 
         inviteContactInfo.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)); 
        } 

        Bitmap photo =null; 


        try { 

         InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), 
           ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(id))); 

         if (inputStream != null) { 

          photo = BitmapFactory.decodeStream(inputStream); 
          inviteContactInfo.setContactImage(photo); 

         } 
         else 
         { 
          photo =BitmapFactory.decodeResource(getResources(), R.drawable.profilepic);; 
          inviteContactInfo.setContactImage(photo); 
         } 

         assert inputStream != null; 
         inputStream.close(); 

        } catch (Exception e) { 

         e.printStackTrace(); 
        } 


        inviteContactList.add(inviteContactInfo); 
       } 
      } 

      InviteContactAdapter contactAdapter = new InviteContactAdapter(getApplicationContext(), inviteContactList); 
      recList.setLayoutManager(new LinearLayoutManager(this)); 
      recList.setAdapter(contactAdapter); 
     } 
    } 
+0

Dank für die Antwort, Wie benutze ich es in meinem ... ?? Ich habe zwei Klassen, bitte versuchen und beheben..Ich bin stecken – Meghana

+0

das Problem mit dem Code, den ich gesendet habe ist ... wenn ich es ausführen ..Es kommt in Kontakt Telefonbuch und hat eine Bestimmung, mehrere Kontakte auszuwählen, aber nicht Anzeige ... aber bei Einzelkontaktauswahl Absicht i = neue Absicht (Intent.ACTION_GET_CONTENT, ContactsContract.CommonDataKinds.Phone.CONTENT_URI); startActivityForResult (i, RESULT_PICK_CONTACT); Es funktioniert gut. – Meghana

+0

In meinem Beispielcode gebe ich die Nummer und den Namen im Cursor-Objekt. Bitte verwenden Sie dieses Cursor-Objekt, um Daten in Ihrem Code zu erhalten. – Dhanaraj

Verwandte Themen