Control to show Cursor with android search engine


I have in my application a part where the user must select a contact from the list of contacts, I could and I managed to bring the list with the phone, like the following image:

This here is a spinner, with a CURSOR as a data source, my question is this: how could I add a search engine in this spinner?

my code so far is

 Spinner imgpayment = (Spinner)findViewById(;

        Cursor mCursor = getContentResolver().query(
                new String[] { ContactsContract.Data._ID, ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER,
                        ContactsContract.CommonDataKinds.Phone.TYPE },
                ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "' AND "
                        + ContactsContract.CommonDataKinds.Phone.NUMBER + " IS NOT NULL", null,
                ContactsContract.Data.DISPLAY_NAME + " ASC");


        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                mCursor, // cursor
                new String[] { ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER }, // cursor

                new int[] {, }


I already tried to use the SpinnerDialog that I found investigating and it appears to me like that

until here everything is perfect, but I can not make it show the name and number, I can only choose one of the 2 and it is unviable, the code of the second image is

   ArrayList<String> contactos = new ArrayList<String>();
        while(!mCursor.isAfterLast()) {
            contactos.add(mCursor.getString(mCursor.getColumnIndex("DISPLAY_NAME"))); //add the item

        spinner = new SpinnerDialog(this,contactos,"Elegir Contacto",3);
        spinner.bindOnSpinerListener(new OnSpinerItemClick() {
            public void onClick(String s, int i) {
                String a = s ;
                String b = s ;


        btnShow = (Button)findViewById(;

        btnShow.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

how could the first add a filtering?

asked by Bruno Sosa Fast Tag 19.10.2017 в 20:11

1 answer


This inconvenience was resolved as explained by this question, Android Studio Filter, ListView With Perzonalized adapter

Inside the adapter creates a method that filters the data according to the text entered by the user. Then simply call that method from your Dialog and pass the text that the user enters.

Your adapter should look like this:

class AdapterMostrarContactos extends BaseAdapter {

protected Activity activity ;
ArrayList<Contacto> contactos;
ArrayList<Contacto> copyContactos = new ArrayList<>();

public AdapterMostrarContactos(Activity activity,  ArrayList<Contacto> contactos){
    this.activity = activity;
    this.contactos = contactos;
    this.copyContactos.addAll(contactos); // Crea una copia de los contactos


/* Filtra los datos del adaptador */
public void filtrar(String texto) {

    // Elimina todos los datos del ArrayList que se cargan en los
    // elementos del adaptador

    // Si no hay texto: agrega de nuevo los datos del ArrayList copiado
    // al ArrayList que se carga en los elementos del adaptador
    if (texto.length() == 0) {
    } else {

        // Recorre todos los elementos que contiene el ArrayList copiado
        // y dependiendo de si estos contienen el texto ingresado por el
        // usuario los agrega de nuevo al ArrayList que se carga en los 
        // elementos del adaptador.
        for (Contacto contacto : copyContactos) {

            if (contacto.getNombre().contains(texto)) {

In your ContactsListDialog class you call the filter method.

public class ContactsListDialog extends Dialog implements DialogInterface.OnClickListener {


private TextWatcher filterTextWatcher = new TextWatcher() {


    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {



answered by 27.10.2017 / 15:45