Search code examples
javaandroidandroid-filterable

Unable to implement Filterable to Object type ArrayList


Am trying to implement Filterable in my recyclerview which contains <Object> here is the code

private Filter exampleFilter = new Filter() {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        ArrayList<Object> filteredList = new ArrayList<>();
        if (constraint == null || constraint.length() == 0) {
            filteredList.addAll(mRecyclerViewItemsAll);
        } else {
            String filterPattern = constraint.toString().toLowerCase().trim();
            for (Object item : mRecyclerViewItemsAll) {
                if (item.getVideo_name().toLowerCase().contains(filterPattern)) {
                    filteredList.add(item);
                }
            }
        }
        FilterResults results = new FilterResults();
        results.values = filteredList;
        return results;
    }
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        mRecyclerViewItems.clear();
        mRecyclerViewItems.addAll((List) results.values);
        notifyDataSetChanged();
    }
};

When i try above code i get cannot resolve method getVideo_name() in Object

So how do i implement Filterable. I have used same code with <String> and it worked.

Recycler view adapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements Filterable {
    private static final int MENU_ITEM_VIEW_TYPE = 0;
    private static final int UNIFIED_NATIVE_AD_VIEW_TYPE = 1;

    private final Context mContext;
    private final ArrayList<Object> mRecyclerViewItems;
    ArrayList<Object> mRecyclerViewItemsAll;

    public RecyclerViewAdapter(Context context, ArrayList<Object> recyclerViewItems) {
        this.mContext = context;
        this.mRecyclerViewItems = recyclerViewItems;
        this.mRecyclerViewItemsAll = new ArrayList<>(recyclerViewItems);
    }

    @Override
    public Filter getFilter() {
        return exampleFilter;
    }
    private Filter exampleFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            ArrayList<Object> filteredList = new ArrayList<>();
            if (constraint == null || constraint.length() == 0) {
                filteredList.addAll(mRecyclerViewItemsAll);
            } else {
                String filterPattern = constraint.toString().toLowerCase().trim();
                for (Object item : mRecyclerViewItemsAll) {
                    if (item.getVideo_name().toLowerCase().contains(filterPattern)) {
                        filteredList.add(item);
                    }
                }
            }
            FilterResults results = new FilterResults();
            results.values = filteredList;
            return results;
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            mRecyclerViewItems.clear();
            mRecyclerViewItems.addAll((List) results.values);
            notifyDataSetChanged();
        }
    };

    public class VideoAdapterViewHolder extends RecyclerView.ViewHolder{
        
    }

    @Override
    public int getItemCount() {
        return mRecyclerViewItems.size();
    }

    @Override
    public int getItemViewType(int position) {
        Object recyclerViewItem = mRecyclerViewItems.get(position);
        if (recyclerViewItem instanceof UnifiedNativeAd) {
            return UNIFIED_NATIVE_AD_VIEW_TYPE;
        }
        return MENU_ITEM_VIEW_TYPE;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        
    }

    private void populateNativeAdView(UnifiedNativeAd nativeAd, UnifiedNativeAdView adView) {
        
}

And here is MainActivity

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.actionbar_menu, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) searchItem.getActionView();
        searchView.setImeOptions(EditorInfo.IME_ACTION_DONE);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                      return false;
                }
              @Override
            public boolean onQueryTextChange(String newText) {
                recyclerViewAdapter.getFilter().filter(newText);
                      return false;
                }
          });
        return true;
    }

Can someone help me what wrong am doing and how to correct it.


Solution

  • cannot resolve method getVideo_name() in Object

    Your list is type of Object which is super class in Java and does not contain method called getVideo_name()

    Object class has predefined method namely:

    toString()
    hashCode()
    clone()
    equals(Object obj)
    getClass()
    finalize()
    wait()
    notify()
    notifyAll()
    

    What you can do is to create your class that is by default subtype of Object class and define class behavious(methods)

    For example:

    class MyData {
        private String name;
    
        public String getVideoName() {
            return this.name;
        }
    
        public void setVideoName(String name) {
            this.name = name;
        }
    
    }
    

    And then use it like :

    private final ArrayList<MyData> mRecyclerViewItems;