Problem with SearchView Actionbar Android

4

I want to see if you can help me with another problem, what happens is that in my Activity where I show Clients in a listView custom, integrate in the part of ActionBar a SearchView for users Can I search the client, I already have my SearchView integrated in my Design and it shows it only when I run the App, only when I try to search for a client it does not search

Code of my Activity and my layout

layout Menu

 <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="@string/action_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always"/>

Activity Metodo Oncreate Menu where the Instanceo and use

   @Override
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_cliente, menu);

        final MenuItem searchItem = menu.findItem(R.id.action_search);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        //permite modificar el hint que el EditText muestra por defecto
        searchView.setQueryHint(getText(R.string.search));
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (TextUtils.isEmpty(newText)) {
                    adaptador.filter("");
                    list.clearTextFilter();
                } else {
                    adaptador.filter(newText);
                }
                return true;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

Code Activity where my custom adapter is

public class MyArrayAdapter extends ArrayAdapter<CXCPSaldoClienteProveedor>{

        private List<CXCPSaldoClienteProveedor> searchList;

        public MyArrayAdapter(Context context, ArrayList<CXCPSaldoClienteProveedor> ArrayClientes)
        {
            super(context, 0, ArrayClientes);
            this.searchList = new ArrayList<>();
            this.searchList.addAll(cliArrayList);
        }


        @Override
        public int getCount() {
            return cliArrayList.size();
        }



        public View getView(int position, View convertView, ViewGroup parent) {

            RecyclerView.ViewHolder holder;

            CXCPSaldoClienteProveedor O_Cliente = getItem(position);

            // Check if an existing view is being reused, otherwise inflate the view
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_cliente, parent, false);
            }


            //Obteniendo instancias de los text views
            TextView nombre = (TextView) convertView.findViewById(R.id.nombrecli);
            TextView saldov = (TextView) convertView.findViewById(R.id.txtsaldov);
            TextView saldot = (TextView) convertView.findViewById(R.id.txtsaldot);
            TextView idcli = (TextView) convertView.findViewById(R.id.clienid);


            //INICIALIZAR FORMAT
            DecimalFormat numberFormat = new DecimalFormat("###,##0.00");

            nombre.setText(O_Cliente.getClienteDescripcion());
            nombre.setTag(O_Cliente);

            saldov.setText(numberFormat.format(O_Cliente.getSaldoVencido()));
            saldot.setText(numberFormat.format(O_Cliente.getSaldo()));

            idcli.setText(String.valueOf(O_Cliente.getCliente()));
            idcli.setTag(O_Cliente);

            // Se almacena en settag el objeto Cliente
            convertView.setTag(O_Cliente);

            //Devolver al ListView la fila creada
            return convertView;
        }

        public void filter(String newText) {
          /*  newText = newText.toLowerCase(Locale.getDefault());
            cliArrayList.clear();
            if (newText.length() == 0) {
                cliArrayList.addAll(searchList);
            } else {
                for (CXCPSaldoClienteProveedor s : searchList) {
                    if (s.toLowerCase(Locale.getDefault()).contains(newText)) {
                        cliArrayList.add(s);
                    }
                }
            }
            notifyDataSetChanged();*/
        }
    }
    
asked by Hugo Rodriguez 19.05.2016 в 18:17
source

1 answer

2

When entering a text, it must be received by the onQueryTextSubmit() method:

@Override
public boolean onQueryTextSubmit(final String query) {
  //Realiza proceso del Asynctask
  //Después de obtener los datos por medio del Asynctask actualizas tu adapter! 
  return true; 
}

You must use onQueryTextSubmit and not onQueryTextChange , this is the difference between both methods:

  

onQueryTextChange (String query) Called when the query text   It is changed by the user.

     

onQueryTextSubmit (Query String) It's called   when the user sends the query.

    
answered by 19.05.2016 / 21:23
source