I'm trying to make a search engine in a RecyclerView, the problem is that I already implemented all the necessary logic that I followed in a tutorial, but when I start writing in the SearchView it doesn't filter the content, I don't understand what I'm doing wrong, yes when I print the ArrayList where I am saving the results if I print the Array data in the console, the problem is that my views continue to see all the queried data.
This is my snippet where I implement the SearchView:
Convenios extends Fragment implements conveniosAdapter.OnEventMovieListener, Response.Listener<JSONObject>, Response.ErrorListener, SearchView.OnQueryTextListener{
RecyclerView recyclerView;
List<com.seccion.seccin15hibrida.model.Convenios> convenios;
conveniosAdapter conveniosadapter;
public static final String TAG = com.seccion.seccin15hibrida.model.Convenios.class.getName();
RequestQueue request;
JsonObjectRequest jsonObjectRequest;
public Convenios(){
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.convenios, container, false);
recyclerView = (RecyclerView)view.findViewById(R.id.recycler_view);
setHasOptionsMenu(true);
convenios = new ArrayList<>();
conveniosadapter = new conveniosAdapter(convenios);
conveniosadapter.setOnEventMovieListener(this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
request = Volley.newRequestQueue(getActivity());
cargarWebService();
return view;
}
private void cargarWebService() {
String url = "http://www.webservice.com/weblist.php/lista.php";
jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,url,null,this,this);
request.add(jsonObjectRequest);
}
@Override
public void onResponse(JSONObject response) {
com.seccion.seccin15hibrida.model.Convenios pacto = null;
JSONArray json = response.optJSONArray("convenio");
try{
for (int i=0; i<json.length(); i++){
pacto = new com.seccion.seccin15hibrida.model.Convenios();
JSONObject jsonObject = null;
jsonObject = json.getJSONObject(i);
pacto.setIdConvenio(jsonObject.optInt("idConvenio"));
pacto.setTituloConvenio(jsonObject.optString("tituloConvenio"));
pacto.setSubtituloConvenio(jsonObject.optString("subtituloConvenio"));
pacto.setImagenConvenio(jsonObject.optString("imagenConvenio"));
pacto.setVigenciaConvenio(jsonObject.optString("vigenciaConvenio"));
pacto.setDescripcionConvenio(jsonObject.optString("descripcionConvenio"));
pacto.setUrlConvenios(jsonObject.optString("linkConvenio"));
convenios.add(pacto);
}
conveniosAdapter pactoAdapter = new conveniosAdapter(convenios);
recyclerView.setAdapter(pactoAdapter);
conveniosadapter.notifyDataSetChanged();
}catch (JSONException e){
Toast.makeText(getActivity(),"No hay ningun convenio para mostrar, intentalo mas tarde", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(),"No se pudo consultar la informacion, intentalo nuevamente", Toast.LENGTH_LONG).show();
Log.i("Error","No se pudo consultar la informacion, intentalo nuevamente");
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater().inflate(R.menu.menu_busqueda, menu);
MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView)myActionMenuItem.getActionView();
searchView.setOnQueryTextListener(this);
}
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
conveniosadapter.getFilter().filter(s);
conveniosadapter.notifyDataSetChanged();
return true;
}
}
In fact, if I print what the onQueryTextChange(String s) function returns, it prints what I'm writing, and this is my adapter where I implement the search:
public class conveniosAdapter extends RecyclerView.Adapter<conveniosAdapter.ConveniosViewHolder> implements Filterable {
List<Convenios> convenios;
private List<Convenios> filteredList;
private OnEventMovieListener onEventMovieListener;
Context context;
public conveniosAdapter(List<Convenios> convenios){
this.convenios = convenios;
this.filteredList = convenios;
}
@Override
public ConveniosViewHolder onCreateViewHolder(ViewGroup parent, int typeView){
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_convenios, parent, false);
conveniosAdapter.ConveniosViewHolder conveniosVH = new conveniosAdapter.ConveniosViewHolder(itemView, filteredList);
context = parent.getContext();
return conveniosVH;
}
@Override
public void onBindViewHolder(ConveniosViewHolder holder, int position) {
final Convenios conve = filteredList.get(position);
holder.tituloConvenio.setText(conve.getTituloConvenio());
holder.subtituloConvenio.setText(conve.getSubtituloConvenio());
final String url = new String(conve.getImagenConvenio());
if(url.isEmpty()){
Picasso.with(context).load(R.drawable.nofound).into(holder.imagenConvenio);
}else {
Picasso.with(context).load(url).into(holder.imagenConvenio);
}
holder.vigenciaConvenio.setText(conve.getVigenciaConvenio());
holder.urlConvenios.setText(conve.getUrlConvenios());
holder.descripcionConvenio.setText(conve.getDescripcionConvenio());
}
@Override
public int getItemCount() {
return filteredList.size();
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
Log.i("TEXTO","Consulta: "+charString);
filteredList = convenios;
} else {
List<Convenios> newFilteredList = new ArrayList<>();
for (Convenios convenios : convenios) {
if (convenios.getTituloConvenio().toLowerCase().contains(charString)) {
newFilteredList.add(convenios);
}
}
filteredList = newFilteredList;
Log.i("FLY","Contenido: "+filteredList);
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
filteredList = (List<Convenios>) filterResults.values;
notifyDataSetChanged();
}
};
}
public class ConveniosViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener, View.OnClickListener{
TextView tituloConvenio;
TextView subtituloConvenio;
ImageView imagenConvenio;
TextView vigenciaConvenio;
TextView urlConvenios;
TextView descripcionConvenio;
Button showConvenios;
List<Convenios> convenios;
public ConveniosViewHolder(View itemView, List<Convenios> convenios){
super(itemView);
this.convenios = convenios;
tituloConvenio = (TextView)itemView.findViewById(R.id.tituloConvenio);
subtituloConvenio = (TextView)itemView.findViewById(R.id.subtituloConvenio);
imagenConvenio = (ImageView)itemView.findViewById(R.id.imagenConvenio);
vigenciaConvenio = (TextView)itemView.findViewById(R.id.vigenciaConvenio);
urlConvenios = (TextView)itemView.findViewById(R.id.urlConvenios);
descripcionConvenio = (TextView) itemView.findViewById(R.id.descripcionConvenio);
showConvenios = (Button)itemView.findViewById(R.id.showConvenios);
showConvenios.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.showConvenios:
Convenios news = filteredList.get(getAdapterPosition());
Intent intent = new Intent(context, DescripcionConvenios.class);
intent.putExtra("url",news.getUrlConvenios());
intent.putExtra("titulo",news.getTituloConvenio());
context.startActivity(intent);
break;
default:
break;
}
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
}
}
public interface OnEventMovieListener{
}
public OnEventMovieListener getOnEventMovieListener() {
return onEventMovieListener;
}
public void setOnEventMovieListener(OnEventMovieListener onEventMovieListener) {
this.onEventMovieListener = onEventMovieListener;
}
}
If I print what the For returns within the search function, which is this block:
for (Convenios convenios : convenios) {
if (convenios.getTituloConvenio().toLowerCase().contains(charString)) {
newFilteredList.add(convenios);
}
}
filteredList = newFilteredList;
Log.i("FLY","Contenido: "+filteredList);
This is what it prints for me:
2019-09-05 17:54:12.909 23578-23929/? I/FLY: Content: [com.seccion.seccion15hibrida.model.Convenios@58cec6f, com.seccion.seccion15hibrida.model.Convenios@9e4ad7c, com.seccion.seccion15hibrida.model.Convenios@d21d605] 2019-09-05 17: 54:15.684 23578-23932/? I/FLY: Content: [com.seccion.seccion15hibrida.model.Convenios@58cec6f] 2019-09-05 17:54:16.067 23578-23934/? I/FLY: Content: [com.seccion.seccion15hibrida.model.Convenios@58cec6f] 2019-09-05 17:54:16.312 23578-23936/? I/FLY: Content: [com.seccion.seccion15hibrida.model.Convenios@58cec6f] 2019-09-05 17:54:16.512 23578-23938/? I/FLY: Content: [com.seccion.seccion15hibrida.model.Convenios@58cec6f] 2019-09-05 17:54:18.193 23578-23941/? I/FLY: Content: [com.seccion.seccion15hibrida.model.Convenios@58cec6f] 2019-09-05 17:54:18.343 23578-23943/? I/FLY: Content: [com. section.seccion15hibrida.model.Convenios@58cec6f] 2019-09-05 17:54:18.579 23578-23945/? I/FLY: Content: [com.seccion.seccion15hibrida.model.Convenios@58cec6f]
What I understand are the results that are filtered but in my view all the results are seen. it doesn't update me or filter what i'm looking for
In the class where the SearchView is implemented in the onQueryTextChange(String s) method, what I was missing was to pass the new adapter with the searched values to the RecyclerView, being this way: