creating generalize search for realm. I have RecyclerView
which could contain different string values as a field from different class.
i have implemented a search functionality using SearchView
and SearchManager
issue that i am facing is the repetition of code. Recyclerview
is having string values from different class i.e. CallReasonInfo , ObsercationInfo etc.
now i had to create different search queries to search records in all these class as blow.
i was looking for an approach where in i dont need to create / add listener for each specific type of RealmResults
lookupSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
RealmResults<CallReasonInfo> searchCallReasonInfo =
getCallReasonInfoQuery()
.contains("callReasonName",query)
.findAll();
searchCallReasonInfo.addChangeListener(new RealmChangeListener<RealmResults<CallReasonInfo>>() {
@Override
public void onChange(RealmResults<CallReasonInfo> elements) {
adapter.setSearchListAdapter(elements);
}
});
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
Log.e(TAG , "onQueryTextChange "+newText);
RealmResults<CallReasonInfo> searchCallReasonInfo =
getCallReasonInfoQuery()
.contains("callReasonName",newText.trim(), Case.INSENSITIVE)
.findAllAsync();
searchCallReasonInfo.addChangeListener(new RealmChangeListener<RealmResults<CallReasonInfo>>() {
@Override
public void onChange(RealmResults<CallReasonInfo> elements) {
adapter.setSearchListAdapter(elements);
}
});
return true;
}
});
in adapter i have following function
public void setSearchListAdapter(RealmResults<E> results){
lookupInfo = results;
notifyDataSetChanged();
}
based on the ListAdapter code written below i have to use the query and not the filterable
public class SearchListAdapter <E extends RealmModel> extends RecyclerView.Adapter<SearchListAdapter.SearchListViewHolder>
implements View.OnClickListener {
private static final String TAG = "SearchListAdapter";
private RealmResults<E> lookupInfo;
private static String lookupType;
private static ItemClickListener itemClickListener;
public SearchListAdapter( RealmResults<E> resultInfo, String lookupType, ItemClickListener listener) {
this.lookupType = lookupType;
lookupInfo = resultInfo;
itemClickListener = listener;
}
@Override
public SearchListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.seach_list_item, parent, false);
SearchListAdapter.SearchListViewHolder viewHolder = new SearchListAdapter.SearchListViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(SearchListViewHolder holder, int position) {
holder.bind(lookupInfo.get(position));
}
@Override
public int getItemCount() {
return lookupInfo==null ? 0 : lookupInfo.size();
}
@Override
public void onClick(View v) {
Log.e(TAG , "Clicked "+v.getTag());
}
public static class SearchListViewHolder extends RecyclerView.ViewHolder {
private TextView tvItemName;
public SearchListViewHolder(View itemView) {
super(itemView);
tvItemName = (TextView) itemView.findViewById(R.id.tvItemName);
}
public void bind(final RealmModel item){
}
}
public void setSearchListAdapter(RealmResults<E> results){
lookupInfo = results;
notifyDataSetChanged();
}
}