0

creating generalize search for realm. I have RecyclerViewwhich 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();
    }



}
Hunt
  • 8,215
  • 28
  • 116
  • 256
  • 1
    extend [RealmBaseAdapter](https://github.com/realm/realm-android-adapters/blob/master/adapters/src/main/java/io/realm/RealmBaseAdapter.java) and implement `Filterable` interface in your custom adapter class – pskink Feb 15 '17 at 14:18
  • http://stackoverflow.com/a/40632613/2413303 – EpicPandaForce Feb 15 '17 at 14:48
  • @EpicPandaForce this is wrong: filtering is done in `publishResults()`, it should of course be done inside `performFiltering()` – pskink Feb 15 '17 at 17:06
  • @pskink how exactly do you intend to [pass the managed RealmResults between `performFiltering`''s background thread to the UI thread?](https://realm.io/docs/java/latest/#threads) Go on, I'm curious. – EpicPandaForce Feb 15 '17 at 17:07
  • @EpicPandaForce so use `findAllAsync` otherwise it kills the `Filter`s whole idea of searching in a worker thread - in your case all the worki is done in UI thread – pskink Feb 15 '17 at 17:21
  • Well yes, you can use `findAllAsync()` in place of `findAll()`, the RealmRecyclerViewAdapter handles that case too - you still need to do that in `publishResults` – EpicPandaForce Feb 15 '17 at 17:56
  • @EpicPandaForce unfortunately `publishResults` is too late for `Filter` to work properly: `findAllAsync` has to be called on the UI thread but **before** `performFiltering` finishes (and `performFiltering` has to be suspended until the query finishes), i know it is a bit complex but otherwise there is really no point of using `Filter` (of course all above is true if there is really no way of passing `RealmResults` between threads - i have limited knowledge of that unfortunately) – pskink Feb 15 '17 at 21:36
  • @pskink technically you get integration with `SearchView`, that is something – EpicPandaForce Feb 15 '17 at 22:50
  • Have updated the question by added listadapter code – Hunt Feb 16 '17 at 10:36
  • @Hunt Have you considered to use `DynamicRealmObject`? – geisshirt Mar 03 '17 at 11:43
  • Never came across `DynamicRealmObject` – Hunt Mar 03 '17 at 12:04
  • @Hunt You might be able to write in a more generic way - but you loose speed and type-safety (in your IDE). – geisshirt Mar 06 '17 at 12:00
  • How can i loose speed ? – Hunt Mar 06 '17 at 15:52
  • The dynamic objects will check if a property exists while Realm generated proxy classes for the model classes and you only have getters/setters for properties you have in the Realm file. Said that, even dynamic objects are fast :-) – geisshirt Mar 07 '17 at 08:14
  • so you mean if i use more generalize code using reflection the way i did in `SearchListAdapter` , it will slow down the performance ? – Hunt Mar 07 '17 at 16:23

0 Answers0