How do I filter a search using Searchview?

Asked

Viewed 387 times

1

I’m building an app where in a particular app a list of registered users will appear in a ListView. This Activity also has a SearchView which serves to filter data from ListView according to the written text. I would like to know how I can make this information filter.

Searchview xml code:

<android.support.v7.widget.CardView
                android:id="@+id/sv_card_aux"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="60dp"
                android:layout_marginEnd="10dp"
                android:layout_marginBottom="10dp"
                android:clickable="true"
                android:focusable="true"
                android:foreground="?android:attr/selectableItemBackground"
                app:cardBackgroundColor="@color/white_smoke"
                app:cardCornerRadius="5dp"
                app:cardUseCompatPadding="true">

                <android.support.v7.widget.SearchView
                    android:id="@+id/sv_aux"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </android.support.v7.widget.CardView>

Activity Code

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_contas_auxiliares);
  listaContasAux = (ListView) findViewById(R.id.listaAuxContas);
  svContasAux = (SearchView) findViewById(R.id.sv_aux);
  svCardContasAux = (CardView) findViewById(R.id.sv_card_aux);

  svCardContasAux.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                svContasAux.setIconified(false);
            }
        });
}

Activity with the SearchView:

inserir a descrição da imagem aqui

NOTE: I tried to use a method to filter the search in Adapter, but could not, ag

1 answer

1

Thanks to those who saw the post, however I and a friend already found a solution that consists of putting the information that is stored on ListView in a ArrayList, I’ll show you the code:

//Cria a variável usuariosArray na classe Adapter
private ArrayList<Usuarios> usuariosArray;

Adapter Instance:

public AuxliaresAdapter(Context ctx, List<Usuarios> lista) {
        this.contexto = ctx;
        this.listaUsuarios = lista;
        this.usuariosArray = new ArrayList<Usuarios>();
        this.usuariosArray.addAll(lista);
    }

Search filter method in Adapter:

public void myFilter(String name){
        name = name.toLowerCase(Locale.getDefault());
        listaUsuarios.clear();
        if(name.length() == 0){
            listaUsuarios.addAll(usuariosArray);
        }else{
            for (Usuarios usuarios :  usuariosArray){
                if(usuarios.getEmail().toLowerCase(Locale.getDefault()).contains(name)){
                    listaUsuarios.add(usuarios);
                }
            }
        }
        notifyDataSetChanged();
    }

Code of activity containing the ListView and the SearchView:

svContasAux.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                auxliaresAdapter.myFilter(s.toLowerCase());
                return false;
            }
        });

Browser other questions tagged

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