Tengo un recyclerview que carga datos de una clase modelo, y al implementar un filtro no hace nada. Los datos los descargo desde una web con volley y las guardo en una bd sqlite (todo esto desde un Fragment). Aqui mi codigo:
Fragment
public class InicioFragment extends Fragment implements Response.Listener<JSONArray>,Response.ErrorListener, SearchView.OnQueryTextListener{
ArrayList<MyModel> lista;
RecyclerView recyclerView;
Activity activity;
IFragments interface_frag;
DataAdapter dataAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_inicio, container, false);
lista=new ArrayList<>();
recyclerView= (RecyclerView) rootView.findViewById(R.id.list_m);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
con = new conexion(getContext());
request = Volley.newRequestQueue(getContext());
new cargar_data().execute();
dataAdapter = new DataAdapter(lista, getContext());
recyclerView.setAdapter(dataAdapter);
mostrar_lista();
return rootView;
}
private void mostrar_lista() {
try {
int id = getFromSharedPref("Id");
bd = con.getReadableDatabase();
String[] par = {String.valueOf(id)};
String[] campos = {TablaUser.Id, TablaUser.Tipo, TablaUser.IdRegion};
Cursor cursor = bd.query(TablaUser.TablaUser, campos, TablaUser.Id + "=?", par, null, null, null);
int id = 0;
String tipo = "";
int idregion = 0;
if (cursor.moveToFirst()) {
id = cursor.getInt(0);
tipo = cursor.getString(1);
idregion = cursor.getInt(2);
String url = "http://www.xxx.mx/yyy/zzz.svc/GetAll/" + id + "/" + tipo + "/" + idregion + "";
url = url.replace(" ", "%20");
jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, this, this);
jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(MY_DEFAULT_TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
request.add(jsonArrayRequest);
} else {
Toast.makeText(getContext(), "No se encontraron datos", Toast.LENGTH_SHORT).show();
}
cursor.close();
bd.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private int getFromSharedPref(String id) {
SharedPreferences sharedPreferences = getActivity().getSharedPreferences("Login_pref", getContext().MODE_PRIVATE);
return sharedPreferences.getInt(id, 0);
}
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "ERROR " + error.toString(), Toast.LENGTH_SHORT).show();
System.out.println();
Log.d("ERROR ", error.toString());
}
@Override
public void onResponse(JSONArray response) {
MyModel model = null;
if (response != null) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject jsonObject = null;
jsonObject = response.getJSONObject(i);
int Id = jsonObject.optInt("Id");
String Nombre = jsonObject.optString("Nombre");
String Fecha = jsonObject.optString("Fecha");
...etc
model = new ProdMuestreo(Id, etc);
bd = con.getReadableDatabase();
String[] par = {String.valueOf(Id)};
String[] campos = {_mymodel.id};
Cursor cursor = bd.query(_mymodel.TDatos, campos, _mymodel.Id + "=?", par, null, null, null);
if (!cursor.moveToFirst()) {
bd.close();
bd = con.getWritableDatabase();
con.insert(Id,etc);
bd.close();
}
cursor.close();
bd.close();
lista.add(muestreos);
}
DataAdapter dataAdapter = new DataAdapter(lista);
recyclerView.setAdapter(dataAdapter);
} catch (JSONException e) {
Toast.makeText(getContext(), "Error: " + e.toString(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
} else {
Toast.makeText(getContext(), "No se encontraron datos", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
dataAdapter.getFilter().filter(newText);
return false;
}
}
Adapter
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.DataHolder> implements View.OnClickListener, Filterable
{
private View.OnClickListener listener;
ArrayList<MyModel> list;
ArrayList<MyModel> listAll;
IFragments interface_frag;
RecyclerView recyclerView;
Context context;
public DataAdapter(ArrayList<MyModel> list, Context context)
{
this.list=list;
this.listAll=new ArrayList<>(list);
this.context = context;
}
@NonNull
@Override
public DataHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.items_inicio,null,false);
RecyclerView.LayoutParams layParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(layParams);
view.setOnClickListener(this);
return new DataHolder(view);
}
@Override
public void onBindViewHolder(@NonNull DataHolder holder, final int position) {
holder.nom.setText("Nombre: "+lista.get(position).getNombre().toString());
holder.solic.setText("Solicita: "+ lista.get(position).getSolicitante().toString());
holder.fecha.setText("Fecha: "+ lista.get(position).getFecha());
}
@Override
public int getItemCount() {
if (list != null)
{
return list.size();
}
return 0;
}
public void setOnClickListener (View.OnClickListener listener){
this.listener=listener;
}
@Override
public void onClick(View v) {
if(listener!=null){
listener.onClick(v);
}
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
ArrayList<MyModel> filteredList = new ArrayList<>();
if (constraint.toString().isEmpty()) {
filteredList.addAll(listMuestreosAll);
} else {
for (MyModel m : listMuestreosAll) {
if (m.getNombre().toLowerCase().contains(constraint.toString().toLowerCase())) {
filteredList.add(m);
}
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults filterResults) {
list.clear();
list.addAll((Collection<? extends MyModel>) filterResults.values);
notifyDataSetChanged();
}
};
}
public class DataHolder extends RecyclerView.ViewHolder{
TextView text_ing,txt_cod,txt_fecha;
public DataHolder(View itemView) {
super(itemView);
nom=(TextView) itemView.findViewById(R.id.nombre);
solic=(TextView) itemView.findViewById(R.id.solic);
fecha=(TextView) itemView.findViewById(R.id.fecha);
}
}
Mi clase modelo
public class MyModel implements Serializable {
private int Id;
public String Nombre;
private String Solicitante;
private String Fecha;
public MyModel(int id, String Nombre, String Solicitante, String Fecha)
{ Id = id;
Nombre = Nombre;
Solicitante = Solicitante;
Fecha = Fecha;
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getNombre() {
return Nombre;
}
public void setNombre(String Nombre) {
Nombre = Nombre;
}
public String getSolicitante() {
return Solicitante;
}
public void setSolicitante(String Solicitante) {
Solicitante = Solicitante;
}
public String getFecha() {
return Fecha;
}
public void setFecha(String Fecha) {
Fecha = Fecha;
}
}
getFilter()