2015-10-05 98 views
2
與聯繫人選擇器有問題

I'm,它的工作原理與Phone.DISPLAY_NAME和Phone.NUMBER但它不`噸與Email.ADDRESSAndroid的聯繫人選擇器獲取名稱+編號+電子郵件

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.CommonDataKinds.Phone.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 usign multiple startActivityForReslut 
     switch (requestCode) { 
     case CONTACT_PICKER: 
      contactPicked(data); 
      break; 
     } 
    } else { 
     Log.e("MainActivity", "Failed to pick contact"); 
    } 
} 
private void contactPicked(Intent data) { 
    Cursor cursor = null; 
    try { 
     String mail = null ; 
     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 email 
     int mailIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS); 
     // column index of the contact name 
     int nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 
     mail = cursor.getString(mailIndex); 
     phoneNo = cursor.getString(phoneIndex); 
     name = cursor.getString(nameIndex); 
     // Set the value to the textviews 
     txtMailContacto.setText(mail); 
     txtTelefono.setText(phoneNo); 
     txtNombreContacto.setText(name); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
} 
工作

所以,我試圖讓這些領域,但我無法弄清楚是什麼問題。 感謝您的幫助...

回答

4

明白了!!!

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(); 
    try { 
     // getData() method will have the Content Uri of the selected contact 
     Uri uri = data.getData(); 
     //Query the content uri 
     cur = cr.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(); 
    } 
} 
} 
+2

你的第一個查詢似乎是無用的? 'Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);' – behelit

+0

@behelit同意!我測試它沒有最初的查詢,它工作正常。編輯答案並將其刪除。 –

+1

現在您已經刪除了Cursor cur,代碼不完整 –

相關問題