Here is the code I am not able to implement search in heterogeneous recyclerview with two different viewholders. Can anyone suggest me something. Moreover my views of recyclerview are textViews and cardViews. CardView is having data written on it for example name, age and phone number. I want to search with the name and phone number. Nothing with the date textView.
Thanks in advance.
Here is my code :
if (charString.isEmpty()) {
// Search is empty/Nothing Entered
mAppointmentsList = mAppointmentsListFiltered;
} else {
List<DataAppointments> filterResults = new ArrayList<>();
for (DataAppointments data : mAppointmentsList) {
if (data instanceof DataPatientDetails) {
DataPatientDetails details = (DataPatientDetails) data;
if (details.getName().toLowerCase().contains(charString.toLowerCase()) ||
details.getPhone().toLowerCase().contains(charString.toLowerCase()) ||
details.getId().toLowerCase().contains(charString.toLowerCase())) {
filterResults.add(data);
if (BuildConfig.DEBUG)
Log.d(TAG, "performFiltering: " + details.getName());
}
}
}
mAppointmentsListFiltered = filterResults;
if (BuildConfig.DEBUG)
Log.d(TAG, "performFilteringPatientListFiltered: " + mAppointmentsListFiltered);
}
FilterResults results = new FilterResults();
results.values = mAppointmentsListFiltered;
results.count = mAppointmentsListFiltered.size();
if (BuildConfig.DEBUG)
Log.d(TAG, "performFilteringResultsUpcoming: " + results.count);
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mAppointmentsListFiltered = (ArrayList<DataAppointments>) results.values;
if (BuildConfig.DEBUG) Log.d(TAG, "publishResults: " + mAppointmentsListFiltered);
notifyDataSetChanged();
}
};
}`
And my onQueryChangeListener :
// Query Search in Material Search view
private void querySearch() {
if (getActivity() != null)
mSearchView = getActivity().findViewById(R.id.search_view_upcoming);
mSearchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
if (BuildConfig.DEBUG) Log.d(TAG, "onQueryTextSubmit: " + query);
mAdapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (BuildConfig.DEBUG) Log.d(TAG, "onQueryTextChange: " + newText);
mAdapter.getFilter().filter(newText);
if (BuildConfig.DEBUG) Log.d(TAG, "onQueryTextChange: " + mAdapter.getItemCount());
return false;
}
});
}
Based on the code you posted.
in the else statement, I am not exactly sure what is being compared. You can have condition to check if DataAppointments
is of type DataPatientDetails
and then do your comparision. I am assuming mAppointmentsList
is an actual list of data.
else {
for (DataAppointments data : mAppointmentsList) {
if (data instanceof DataPatientDetails) {
DataPatientDetails details = (DataPatientDetails) data;
// do your comparision here, if it passes, then add to the filtered results
}
}
}