Recover Contact List Name List on an android Edit

Asked

Viewed 378 times

0

All right, guys ? I have a problem in the development of an app, because when I call his registration screen before I want to take the name and phone of the selected contact and send to the Activity Edit, but I can’t recover the name and neither at the same time, just the number, would you please help me get them both back at the same time? follows the code I use to recover the number and that 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);

            }
  • It would be better to post what you tried to do, because in theory there is no reason not to be able to get the 2 data at the same time, since it is a Contentprovider that returns a Cursor.

  • In your projected array, you passed the number and contact?

  • I tried several ways but I am already without ideas of how to do, the last attempt was this, trying to recover it inside the projection array along with the name, remembering that the code I sent above works perfectly to bring the number:

1 answer

0


The column of ContactsContract that you should use to recover the name is this:

ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME

Then you can try to do as it was already encoded 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();    
        }
    }
}
  • Boy, that’s all it was!!! It worked well for me!!! Thank you Marcion !!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.