2

I can't click RecyclerView to a new Activity from RecyclerViewAdapter.

I call ItemClick here.

DayAdapter.java:

holder.setItemClickListener(new ItemClickListener() {
        @Override
        public void onClick(View view, int position, boolean isLongClick) {
            openProgramActivity(view, position);
        }
    });
}

This function opens a new Activity:

public void openProgramActivity(View view, int position) {
    //Intent openProgramActivity = new Intent(context, ProgramActivity.class);
    Intent openProgramActivity = new Intent(view.getContext(), ProgramActivity.class);
    openProgramActivity.putExtra("index",position);
    view.getContext().startActivity(openProgramActivity);
}

FragmentDay30.java:

public class FragmentDay30 extends Fragment {

private View view;

public static FragmentDay30 newInstance() {
    FragmentDay30 fragment = new FragmentDay30();
    Bundle args = new Bundle();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragment_30day, container,false);

    ViewPager slideViewPager = (ViewPager) view.findViewById(R.id.slideViewPager);
    SlideAdapter slideAdapter = new SlideAdapter(getActivity());
    slideViewPager.setAdapter(slideAdapter);

    RecyclerView fragment30datRecyclerView = (RecyclerView) view.findViewById(R.id.fragment30dayRecyclerView);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
    fragment30datRecyclerView.setLayoutManager(linearLayoutManager);
    DayAdapter dayAdapter = new DayAdapter(getActivity());
    fragment30datRecyclerView.setAdapter(dayAdapter);

    return view;
}

I try to use getActvity() and getContext() but not to new Activity.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • 1
    https://stackoverflow.com/questions/49492474/how-to-use-get-getapplicationcontext-in-adapter-class/49492494#49492494 – AskNilesh Jul 07 '18 at 04:21

3 Answers3

7

Pass contaxt to recyclerview adapter constructor like this

Context context;
MyAdapter(Context context, .....){
this.context=context;
}

Call Activity

context.startActivity(......);
Gursewak Singh
  • 280
  • 1
  • 4
  • Inside - public void onBindViewHolder : holder.ivArtistImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("myTag", "This is my message"); Context context = v.getContext(); Toast.makeText(context, "clicked on " +position, Toast.LENGTH_SHORT).show(); } } This works for me! – Toby Feb 09 '21 at 12:10
  • Thanks! This saved my Day!! context.startActivity(......); fixed the issue – Mithun S Jul 17 '21 at 07:11
2

Though you can start Activity from Adapter class passing a Context but as Documented it's

  1. not a Good design pattern
  2. and also a Bad practice to follow.

I would rather suggest to have an interface defined in your Adapter class which would be implemented by the Fragment class. Fragment class initializes the Adapter passing it's reference which you would typeCast to interface like this

DayAdpater.class

public class DayAdapter extends RecyclerView.Adapter<DayAdapter.ViewHolder> {

    private OnActionListener mListener;

    DayAdapter(OnActionListener listener){
        this.mListener=listener;
    }

     holder.setItemClickListener(new ItemClickListener() {
        @Override
        public void onClick(View view, int position, boolean isLongClick) {
                mListener.startActivity(position);
        }
    });

    interface OnActionListener{
        public void startActivity(int position);
    }

}

FragmentDay30.class

public class FragmentDay30 extends Fragment implements DayAdapter.OnActionListener{

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_30day, container,false);

        RecyclerView fragment30datRecyclerView = (RecyclerView) view.findViewById(R.id.fragment30dayRecyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        fragment30datRecyclerView.setLayoutManager(linearLayoutManager);
        DayAdapter dayAdapter = new DayAdapter(getActivity(), this);
        fragment30datRecyclerView.setAdapter(dayAdapter);

        return view;
    }

    /**
    * this is the place where you should start a new activity
    */
    public void startActivity(int position) {
        //Intent openProgramActivity = new Intent(context, ProgramActivity.class);
        Intent openProgramActivity = new Intent(getActivity(), ProgramActivity.class);
        openProgramActivity.putExtra("index",position);
        getActivity.startActivity(openProgramActivity);
    }

}

This is how the your adpater class interacts with the fragment class.

Hope this helps.

Mayank Bhatnagar
  • 2,120
  • 1
  • 12
  • 20
0

Pass context in RecyclerView adapter constructor which you are using for setAdapter like this:

Context context;

MyCustomAdapter(Context context, .....){
this.context=context;
}

For call Activity used:

context.startActivity(......);

Marilia
  • 1,911
  • 1
  • 19
  • 28