Search code examples
androidandroid-fragmentsandroid-recyclerviewfragmentsearchview

Calling an adapter method in a fragment


Friends , I have a fragment with a search-view and recyclerview in it. I want to implement that search-view on the recyclerview data.I am facing problem when i try to call a method from my adapter class in my fragment it does not allow it and asks me to make the method in adapter class as static which is not fine.Here's the required fragment code -

 private void processQuery(String query) {
        // in real app you'd have it instantiated just once
        ArrayList<String> result = new ArrayList<>();

        // case insensitive search
        for (String category : category_name) {
            if (category.toLowerCase().contains(query.toLowerCase())) {
                result.add(category);
            }
        }

        (MyCategoryAdaptercheckbox) MyAdapter.setCategorynames(result);
    }

I am getting error on this line-

(MyCategoryAdaptercheckbox) MyAdapter.setCategorynames(result);

Here's my MyAdaper declaration and initialization -

RecyclerView.Adapter MyAdapter;
 MyAdapter = new MyCategoryAdaptercheckbox(getMyCategoryAdapter1, getActivity().getApplicationContext(),category_name);

        recyclerView.setAdapter(MyAdapter);

And here's my adapter class (setCategorynames) method.It is asking me to make this method as static -

public  void setCategorynames(List<String> category_name) {
        this.category_name = category_name;
        notifyDataSetChanged();
    }

So i want to know how can i call a method of adapter class in a fragment? My adapter class code-

public class MyCategoryAdaptercheckbox extends RecyclerView.Adapter<MyCategoryAdaptercheckbox.ViewHolder> {
    List<GetMyCategoryAdapter> getMyCategoryAdapter;
    Context context;
    List<String> category_name;
   GetMyCategoryAdapter getMyCategoryAdapter1;

    public MyCategoryAdaptercheckbox(List<GetMyCategoryAdapter> getMyCategoryAdapter, Context context, List<String> category_name) {
    this.getMyCategoryAdapter = getMyCategoryAdapter;
    this.context = context;
    this.category_name = category_name;
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        if (viewHolder instanceof ViewHolder){
        }
        getMyCategoryAdapter1 =  getMyCategoryAdapter.get(i);
      ((ViewHolder) viewHolder).tv_categorytitle.setText(getMyCategoryAdapter1.getC_name());
        ((ViewHolder) viewHolder).tv_categoryid.setText(getMyCategoryAdapter1.getC_id());
        ((ViewHolder) viewHolder).gt= getMyCategoryAdapter1;
    }

    public void filterList(ArrayList<String> filterdNames) {
        this.category_name = filterdNames;
        notifyDataSetChanged();
    }
    }
}

Solution

  • The one issue i see in your code, that setCategorynames(List<String> category_name) method is not resolved because MyAdapter is not casted to (MyCategoryAdaptercheckbox)

    You need to replace

    (MyCategoryAdaptercheckbox) MyAdapter.setCategorynames(result);

    by this

    ((MyCategoryAdaptercheckbox) MyAdapter).setCategorynames(result);