Remove accent on search using android searchview

Asked

Viewed 146 times

0

I wish that when it was picked up on my listview João were shown João, Joao, joão, joao.

I’m using a searchview, I read something about Normalizer but n understood very well.

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.busca, menu);

        MenuItem menuItem = menu.findItem(R.id.sv);

        SearchView searchView = (SearchView) menuItem.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                arrayAdapter.getFilter().filter(newText);
                return true;
            }
        });

        getMenuInflater().inflate(R.menu.activity_main, menu);
        return super.onCreateOptionsMenu(menu);

    }

2 answers

1


Do it like this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.busca, menu);

    MenuItem menuItem = menu.findItem(R.id.sv);

    SearchView searchView = (SearchView) menuItem.getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
        //Irá tirar não só acentuações mas também qualquer caractere fora de ASCII
        String texto;
             texto = Normalizer.normalize(query, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");

        //seu código

            return false;
        }

       //se for pra passar o texto já modificado para o arrayAdapter, vc faz:
        @Override
        public boolean onQueryTextChange(String newText) {
             String texto;
             texto = Normalizer.normalize(newText, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");

            arrayAdapter.getFilter().filter(texto);
            return true;
        }
    });

    getMenuInflater().inflate(R.menu.activity_main, menu);
    return super.onCreateOptionsMenu(menu);

}
  • Desculpa sou iniciante ainda mas oq eu coloco no lugar de searchTesta vermelho e` text not being used

  • 1

    I fixed, look dnv, in place of the searchT you put the query

  • 1

    There you put the text q whether it is modified

  • 1

    Gave error pq copied d d an aq project and I forgot to change that part before d put here kkkk, dscp there

  • 1

    @Welyson if the answer solved your problem, mark it as accepted so that others who have the same problem can also solve it

  • ok, but I’m having a problem the text is going through Adapter of an arraylist of objects, and I can not pass to the td q I put from "never used"

  • could help me?

  • 1

    @Welyson dude, I don’t quite understand your question, I think I’d better open another question with part of the code, it’s better for understanding

  • 1

    @Welyson n, calm, you speak on onQueryTextChange?

  • yes String como eu passo meu texto aqui;
 aqui= Normalizer.normalize(query, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", ""); I try to pass but no go

  • look this is my code https://answall.com/questions/237899/searchno-listview-n%C3%A3o-show-item-matching

  • 1

    @Welyson laughs mt agr because of the memes, but coming back to the case, you want to pass for the arraylist your text without accents, right?

  • 1

    I’ll modify the answer, look there

  • @Welyson see if that’s what you want

  • unfortunately it didn’t work, but you’re a pretty cool guy I’m going to give up this battle and try to create a favorite vlw system if you want to help me

  • @Welyson Sad...

Show 11 more comments

0

The code of the previous answer works perfectly, however in the case in question it is necessary to convert both this newText String and the String that is inside the Adapter with which you will compare, in my case it was like this:

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.search_only_menu, menu);

        MenuItem searchItem = menu.findItem(R.id.pesquisa_button);
        SearchView searchView = (SearchView) searchItem.getActionView();

        searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);                                       //Troca o botão de buscar para confirmar, já que a busca é em tempo real então não faz muito sentido ter o botão "pesquisar"

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

            @Override
            public boolean onQueryTextChange(String newText) {
                //Convertendo o texto de entrada para caracteres equivalentes sem acentuação, ç etc
                String texto;
                texto = Normalizer.normalize(newText, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "");
                //Chamando o filter
                clienteAdapter.getFilter().filter(texto);
                return false;
            }
        });

        super.onCreateOptionsMenu(menu, inflater);

    }

And inside the Adapter:

@Override
        protected FilterResults performFiltering(CharSequence constraint) {
            List<ClienteEntity> filteredList = new ArrayList<>();

            if(constraint == null || constraint.length() == 0){
                filteredList.addAll(clientesListaFull);                                             //Quando não há nada digitado na pesquisa, mostramos a lista completa (padrão).
            }else{
                //Trasforma a CharSequence em uma String, coloca tudo minúsculo e remove espaços.
                String filterPattern = constraint.toString().toLowerCase().trim();

                for(ClienteEntity cliente : clientesListaFull){                                     //Percorre cliente por cliente da lista de clientes.
                    String nomeNoASCII;
                    nomeNoASCII = Normalizer.normalize(cliente.getName(), Normalizer.Form.NFD)
                            .replaceAll("[^\\p{ASCII}]", "");
                    if(nomeNoASCII.toLowerCase().contains(filterPattern)){                          //Se a CharSequence digitada estiver contida no cliente.getName, adicionamos esse cliente ao resultado da busca.
                        filteredList.add(cliente);
                    }
                }
            }
            FilterResults results = new FilterResults();
            results.values = filteredList;
            return results;
        }

Browser other questions tagged

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