2016-06-23 5 views
0

Ich habe eine Absicht der Kontaktauswahl verwendet. Ich erhalte die Daten des Kontakts durch die Absicht. Durch diese Absicht erhalte ich den Namen, die Nummer der Kontaktperson.Wie bekomme ich E-Mail und Nachname von Kontakten?

Wenn ich versuchte, eine E-Mail-ID des Kontakts abzurufen, wird nur die Nummer des Kontakts anstelle einer E-Mail-ID angezeigt. Auch für den Nachnamen des Kontakts zeigt es immer null an, obwohl der letzte auf den Kontakt gesetzt wurde.

Code:

private void contactPicked(Intent data) { 
    Cursor cursor = null; 
    try { 
     String phoneNo = null ; 
     String name = null; 
     // getData() method will have the Content Uri of the selected contact 
     Uri uri = data.getData(); 
     //Query the content uri 
     cursor = getContentResolver().query(uri, null, null, null, null); 
     cursor.moveToFirst(); 
     // column index of the phone number 
     int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
     // column index of the contact name 
     int nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 

     int contactIdIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID); 

     int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA); 

     int lastNameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME); 


     mOrganizerPhone = cursor.getString(phoneIndex); 
     mOraganizerName = cursor.getString(nameIndex); 
     mOrganizersId = cursor.getString(contactIdIndex); 
     mOrganizersEmail = cursor.getString(emailIndex); 
     mOrganizersLastName = cursor.getString(lastNameIndex); 


    Toast.makeText(PlanEventActivity.this,name+" "+phoneNo,Toast.LENGTH_SHORT).show(); 


    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

Thank you ..

+1

denke ich, dies hilfreich für Sie: http://stackoverflow.com/questions/12109391/getting-name-and-email-from-contact-list-is-very-slow – alway5dotcom

+0

Könnten Sie bitte lassen Sie mich wissen, die Abfrage verwendet werden? Denn während ich gesucht habe, sehe ich die verschiedenen Abfragen für E-Mails und strukturierten Nachnamen. @HuyN – Sid

Antwort

0

Mit "id", können Sie die E-Mail-Adresse wie diese erhalten, ist dies ein Beispiel:

public class CustomerForm extends Activity { 
private final static int CONTACT_PICKER = 1; 
private EditText txtMailContacto; 
private EditText txtNombreContacto; 
private EditText txtTelefono; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_customer_form); 
    txtMailContacto = (EditText) findViewById(R.id.txtMailContacto); 
    txtTelefono = (EditText) findViewById(R.id.txtTelefono); 
    txtNombreContacto = (EditText) findViewById(R.id.txtNombreContacto); 
} 
public void pickContact(View v) 
{ 
    Intent contactPickerIntent = 
      new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
    startActivityForResult(contactPickerIntent, CONTACT_PICKER); 
} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // check whether the result is ok 
    if (resultCode == RESULT_OK) { 
     // Check for the request code, we might be using multiple startActivityForReslut 
     switch (requestCode) { 
     case CONTACT_PICKER: 
      contactPicked(data); 
      break; 
     } 
    } else { 
     Log.e("MainActivity", "Failed to pick contact"); 
    } 
} 
private void contactPicked(Intent data) { 
    ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
    cur.moveToFirst(); 
    try { 
     // getData() method will have the Content Uri of the selected contact 
     Uri uri = data.getData(); 
     //Query the content uri 
     cur = getContentResolver().query(uri, null, null, null, null); 
     cur.moveToFirst(); 
               // column index of the contact ID 
     String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
               // column index of the contact name 
     String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
     txtNombreContacto.setText(name);  //print data    
               // column index of the phone number 
     Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
        new String[]{id}, null); 
        while (pCur.moveToNext()) { 
         String phone = pCur.getString(
           pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
         txtTelefono.setText(phone);   //print data 
        } 
        pCur.close(); 
               // column index of the email 
     Cursor emailCur = cr.query(  
       ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
       null, 
       ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
       new String[]{id}, null); 
      while (emailCur.moveToNext()) { 
       // This would allow you get several email addresses 
        // if the email addresses were stored in an array 
       String email = emailCur.getString(
           emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));           
       txtMailContacto.setText(email);   //print data 
     } 
     emailCur.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
} 
+0

für jede ID muss ich den Cursor für E-Mail erstellen? und was ist mit Nachnamen? @Huy N – Sid

+0

Das stimmt! Über den Nachnamen, meine obige Lösung verwenden "ContactsContract.Contacts.DISPLAY_NAME" und es erhalten den vollen Namen, Sie können Spit-Methode in Java verwenden, um den gewünschten Namen zu bekommen :) – alway5dotcom

+0

Ich habe versucht, diese Lösung keine E-Mail-ID. @Huy N – Sid

Verwandte Themen