Control to show Cursor with android search engine

4

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(R.id.imgpayment);



        Cursor mCursor = getContentResolver().query(
                ContactsContract.Data.CONTENT_URI,
                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");

        startManagingCursor(mCursor);


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

                new int[] { android.R.id.text1, android.R.id.text2 }

        );
        imgpayment.setAdapter(adapter);

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>();
        mCursor.moveToFirst();
        while(!mCursor.isAfterLast()) {
            contactos.add(mCursor.getString(mCursor.getColumnIndex("DISPLAY_NAME"))); //add the item
            mCursor.moveToNext();
        }


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

            }
        });

        btnShow = (Button)findViewById(R.id.Bottton);

        btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                spinner.showSpinerDialog();
            }
        });

how could the first add a filtering?

    
asked by Bruno Sosa Fast Tag 19.10.2017 в 20:11
source

1 answer

5

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
    contactos.clear();

    // 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) {
        contactos.addAll(copyContactos);
    } 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)) {
                contactos.add(contacto);
            }
        }
    }

In your ContactsListDialog class you call the filter method.

public class ContactsListDialog extends Dialog implements DialogInterface.OnClickListener {

...

private TextWatcher filterTextWatcher = new TextWatcher() {

    ...

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
      adapter.filtrar(filterText.getText().toString());
    }

};

}

    
answered by 27.10.2017 / 15:45
source