sucede que tengo un proyecto en android studio el cual realiza una búsqueda en un recyclerview desde un searchview, al depurar mi aplicacion veo que si filtra bien los datos pero al mostrar los datos filtrados en el recyclerview muestra datos que no tienen que ver con la busqueda, aqui mi codigo
MainActivity
public class InicioFragment extends Fragment {
public RecyclerView recyclerView;
public RecyclerView.Adapter adapter;
public RecyclerView.LayoutManager layoutManager;
public SearchView searchView;
public Context context;
public ArrayList<InicioItem> arrayList = new ArrayList<InicioItem>();
public AppConfiguration appConfiguration;
public List<Inicio> listCreditos;
public InicioFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_inicio, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.rclvInicio);
recyclerView.setHasFixedSize(true);
searchView = (SearchView) rootView.findViewById(R.id.searchInicio);
this.context = this.getContext();
appConfiguration = (AppConfiguration) getActivity().getApplicationContext();
arrayList = new ArrayList<InicioItem>();
arrayList = GetArrayListInicioItem();
adapter = new InicioRecyclerAdapter(context, arrayList);
recyclerView.setAdapter(adapter);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
((InicioRecyclerAdapter) adapter).getFilter().filter(newText.toString());
return true;
}
});
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
return rootView;
}
}
Adapter
public class InicioRecyclerAdapter extends RecyclerView.Adapter<InicioRecyclerAdapter.InicioRecyclerViewHolder> implements Filterable {
ArrayList<InicioItem> arrayList = new ArrayList<InicioItem>();
private static ArrayList<InicioItem> arrayListInicio = new ArrayList<InicioItem>();
private ArrayList<InicioItem> inicioItemFilter;
private ArrayList<InicioItem> filteredContactList;
private CustomFilter mFilter;
private LayoutInflater inflater;
private Context context;
public InicioRecyclerAdapter(Context context, ArrayList<InicioItem> arrayList) {
this.context = context;
inflater = LayoutInflater.from(context);
this.arrayList = arrayList;
this.arrayListInicio = arrayList;
this.inicioItemFilter=arrayList;
this.filteredContactList=new ArrayList<>();
this.filteredContactList.addAll(arrayList);
this.mFilter = new CustomFilter(InicioRecyclerAdapter.this);
}
@Override
public Filter getFilter() {
return mFilter;
}
@Override
public int getItemCount() {
return filteredContactList.size();
}
@Override
public InicioRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_inicio_layout, parent, false);//LayoutInflater.from(parent.getContext()) --> inflate
InicioRecyclerViewHolder inicioRecyclerViewHolder = new InicioRecyclerViewHolder(view);
return inicioRecyclerViewHolder;
}
//private class Filtro extends Filter{
public class CustomFilter extends Filter {
private InicioRecyclerAdapter inicioRecyclerAdapter;
private CustomFilter(InicioRecyclerAdapter inicioRecyclerAdapter) {
super();
this.inicioRecyclerAdapter=inicioRecyclerAdapter;
}
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
filteredContactList.clear();
final FilterResults results = new FilterResults();
final String text = charSequence.toString().toLowerCase().trim();
if (text != "" || text.length() != 0) {
for (final InicioItem item : inicioItemFilter) {
if (item.nombrePersona().toLowerCase().contains(text))
{
filteredContactList.add(item);
}
results.values = filteredContactList;
results.count = filteredContactList.size();
}
else
{
filteredContactList.addAll(inicioItemFilter);
}
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
this.inicioRecyclerAdapter.notifyDataSetChanged();
}
}
}