Retrieve contact list name list in an android edit

0

Well, folks? I'm having a problem developing an app, because when I call the application's registration screen before I want to get the name and phone number of the selected contact and send it to the activity edit, however I can not retrieve the name and neither the same time, just the number, could you please help me recover both at the same time? follows the code that I use to retrieve the number and it is currently working:

public void SelecionarContato(){
        Intent contatos = new Intent(Intent.ACTION_PICK); //CHAMANDO UMA ACTIVITY COM A CONSTANTE DE ESCOLHER UM DADO A SER RETORNADO
        contatos.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); //SELECIONANDO O CONTEUDO UTILIZANDO A CONTACTS PROVIDER

        //VALIDANDO
        if (contatos.resolveActivity(getPackageManager()) != null){
            //CHAMO OS CONTATOS 
            startActivityForResult(contatos, REQUEST_SELECT_PHONE_NUMBER);

        }
    }

//TRATANDO O RESULTADO DO RETORNO DOS CONTATOS
    protected void onActivityResult(int RequestCode, int ResultCode, Intent Data){
        if (RequestCode == REQUEST_SELECT_PHONE_NUMBER && ResultCode == RESULT_OK){ //O ULTIMO PARAMETRO É PARA CASO O USUARIO CANCELE
            //Pegar a URI e a Query do contactProvider e o numero do telefone
            Uri contatoUri = Data.getData();
            String[] projecao = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
            Cursor cursor = getContentResolver().query(contatoUri, projecao, null, null, null);

            //SE O CURSOR RETORNAR UM VALOR VALIDO ENTÃO PEGA O NUMERO
            if (cursor != null && cursor.moveToFirst()){
                int indexNumero = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

                String number   = cursor.getString(indexNumero);

                //ação do que recebe o numero do contato e envia para a activity
                telefone.setText(number);
                telefone.setEnabled(false);

            }
    
asked by anonymous 12.08.2017 / 16:00

1 answer

0

The ContactsContract column you should use to retrieve the name is this:

ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME

Then you can try to do as it was already coded before:

protected void onActivityResult(int RequestCode, int ResultCode, Intent Data){
    if (RequestCode == REQUEST_SELECT_CONTACT && ResultCode == RESULT_OK){ //O ULTIMO PARAMETRO É PARA CASO O USUARIO CANCELE
        //Pegar a URI e a Query do contactProvider e o numero do telefone
        Uri contatoUri = Data.getData();
        final String[] projecao = new String[]{
           ContactsContract.CommonDataKinds.Phone.NUMBER, 
           ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
        };

        final int INDEX_NUMBER = 0;
        final int INDEX_NAME = 1;

        Cursor cursor = getContentResolver().query(contatoUri, projecao, null, null, null);

        //SE O CURSOR RETORNAR UM VALOR VALIDO ENTÃO PEGA O NUMERO
        if (cursor != null && cursor.moveToFirst()){
            String number   = cursor.getString(INDEX_NUMBER);
            String name = cursor.getString(INDEX_NAME);

            //ação do que recebe o numero do contato e envia para a activity
            telefone.setText(number);
            telefone.setEnabled(false);
            nome.setText(name);
            cursor.close();    
        }
    }
}
    
14.08.2017 / 01:05