Good to the problem, first I have a Searchview
in my actionbar
.
I have a listview
with a arraydapter
custom which I fill with data that I send from the webservices and I through the class Asynctask
full listiview.
Insert the Searchview
since the user will be able to search for the client by name, only that he already implements the Filter but he does not look for anything and he does not send me an error.
I would like someone to guide me to know what I need to enter.
I leave my code ArrayadApter .
public class MyArrayAdapter extends ArrayAdapter<CXCPSaldoClienteProveedor> implements Filterable{
List<CXCPSaldoClienteProveedor> original;
List<CXCPSaldoClienteProveedor>filtered;
public MyArrayAdapter(Context context, ArrayList<CXCPSaldoClienteProveedor> ArrayClientes) {
super(context, 0, ArrayClientes);
this.original = ArrayClientes;
}
public View getView(int position, View convertView, ViewGroup parent) {
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);
}
//INICIALIZAR FORMAT
DecimalFormat numberFormat = new DecimalFormat("###,##0.00");
((TextView) convertView.findViewById(R.id.nombrecli)).setText(O_Cliente.getClienteDescripcion());
((TextView) convertView.findViewById(R.id.nombrecli)).setTag(O_Cliente);
((TextView) convertView.findViewById(R.id.txtsaldov)).setText(numberFormat.format(O_Cliente.getSaldoVencido()));
((TextView) convertView.findViewById(R.id.txtsaldot)).setText(numberFormat.format(O_Cliente.getSaldo()));
((TextView) convertView.findViewById(R.id.idcliente)).setText(String.valueOf(O_Cliente.getCliente()));
//((TextView) convertView.findViewById(R.id.clienid)).setTag(O_Cliente);
// Se almacena en settag el objeto Cliente
convertView.setTag(O_Cliente);
CXCPSaldoClienteProveedor item = getItem(position);
//Devolver al ListView la fila creada
return convertView;
}
Class filter
@Override
public android.widget.Filter getFilter() {
return new android.widget.Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults filterResults = new FilterResults();
final ArrayList<CXCPSaldoClienteProveedor> Values = new ArrayList<CXCPSaldoClienteProveedor>();
if(constraint != null && original!=null) {
int length=original.size();
int i=0;
while(i<length){
CXCPSaldoClienteProveedor item=original.get(i);
//do whatever you wanna do here
//adding result set output array
Values.add(item);
i++;
}
//following two lines is very important
//as publish result can only take FilterResults objects
filterResults.values = Values;
filterResults.count = Values.size();
}
return filterResults;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
/*List<CXCPSaldoClienteProveedor> data = (List<CXCPSaldoClienteProveedor>) results.values;
notifyDataSetChanged();
clear();
addAll(data);
notifyDataSetInvalidated();*/
original = (ArrayList<CXCPSaldoClienteProveedor>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
};
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
/*List<CXCPSaldoClienteProveedor> data = (List<CXCPSaldoClienteProveedor>) results.values;
notifyDataSetChanged();
clear();
addAll(data);
notifyDataSetInvalidated();*/
original = (ArrayList<CXCPSaldoClienteProveedor>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
Object class
public class CXCPSaldoClienteProveedor implements Serializable {
private int Empresa;
private int Moneda;
private int Cliente;
private String ClienteDescripcion;
private int Proveedor;
private String ProveedorDescripcion;
private double Saldo;
private double SaldoVencido;
public double Importe;
public double Descuento;
public double SubTotal;
public double IVA;
public double Total;
String GPSLocation;
String GPSLocation2;
String Observacion;
public CXCPSaldoClienteProveedor(int Empresa,
int Moneda,
int Cliente,
String ClienteDescripcion,
int Proveedor,
String ProveedorDescripcion,
double SaldoVencido,
double Saldo)
{
super();
this.Empresa = Empresa;
this.Moneda = Moneda;
this.Cliente = Cliente;
this.ClienteDescripcion = ClienteDescripcion;
this.Proveedor = Proveedor;
this.ProveedorDescripcion = ProveedorDescripcion;
this.Saldo = Saldo;
this.SaldoVencido = SaldoVencido;
}
public int getEmpresa()
{
return Empresa;
}
public int getMoneda()
{
return Moneda;
}
public int getCliente()
{
return Cliente;
}
public String getClienteDescripcion()
{
return ClienteDescripcion;
}
public int getProveedor()
{
return Proveedor;
}
public String getProveedorDescripcion()
{
return ProveedorDescripcion;
}
public double getSaldo()
{
return Saldo;
}
public double getSaldoVencido() {
return SaldoVencido;
}
public void setGPSLocation(String value) {GPSLocation=value;}
public String getGPSLocation() {return GPSLocation;}
public void setGPSLocation2(String value)
{
GPSLocation2=value;
}
public String getGPSLocation2() {return GPSLocation2;}
public void setObservacion(String value)
{
Observacion = value;
}
public String getObservacion(){return Observacion;}
public String toLowerCase(Locale aDefault) {
return null;
}
public String toString(){
return ClienteDescripcion;
}
Edit: Solution
After investigating and doing tests, I finally managed to solve the problem to this question, which was that it did not work, I leave the solution that was only a line of code.
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);
if (searchView != null) {
searchView.setQueryHint(getString(R.string.search));
searchView.setMaxWidth(2129960);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
adaptador.getFilter().filter(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
// this is your adapter that will be filtered
System.out.println("on text submit text: " + query);
return true;
}
});
}