Can't resolve the Context or Application while navigating from Adapter of a fragment(A) to another Fragment (B)

Am trying to navigate from one fragment (A) to another (B), but the fragment, but the first fragment (A) has a recyclerView meaning when I click on any Item I should navigate to the next one. Am using android Navigation component but I couldn’t resolve the method findNavController(xxx) since it requires the ApplicationContext of the fragment. , because I tried v.getContext(), v.getApplicationContext(), mContext, but there wasn’t luck.

How can I resolve this issue, below is the onBindViewHolder() in the RecyclerView Adapter class. ?

What could be the best way to reslve this

@Override
    public void onBindViewHolder(@NonNull final CoordinatesViewHolder holder, int position) {

        final Coordinates coord = mCoordinates.get(position);
        holder.place_name.setText(coord.getmUPlaceName());
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               NavHostFragment.findNavController(xxx).navigate(R.id.action_bookmarking_to_weatherFragment);
            }
        });



    }

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

It is not the responsibility of RecyclerView’s adapter to redirect to another fragment. Create interface like public interface OnItemClickListener { void onItemClicked(int position) } Inside your RecyclerView’s adapter add method: public class YourAdapterName extend RecyclerView.Adapter... private OnItemClickListener onItemClickListener void setOnItemClickListener(OnItemClickListener listener) { onItemClickListener = listener } ... @Override public void onBindViewHolder(@NonNull final CoordinatesViewHolder holder, int position) { final Coordinates coord = mCoordinates.get(position); holder.place_name.setText(coord.getmUPlaceName()); holder.view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(onItemClickListener != null) { onItemClickListener.onItemClicked(position) } } }); } In your fragment with recycler, in place where you set adapter add code: YourAdapterClassName adapter = new YourAdapterClassName(...init adapter...)… Read more »