Proper way of calling Activity Method from recycler adapther in Android?

Viewed 1941

I need to call an Intent from the onBindViewHolder of the recycler adapter , on a click event from one of the views in the ViewHolder. I am unable to do so directly ..

    public void onBindViewHolder(MyAdapter.MyViewHolder viewHolder, int i) {
        viewHolder.button.setonClickListener(new View.onClickListener(
               @Override
               onClick(View v) {
               Intent i=new Intent();
                i.setAction("Intent.ACTION_CALL");
                i.setData(Uri.parse("tel:1234567890"));
                startActivity(i);
            }
        ));
}

This did not work. It kept throwing error. I then created a public method in the Activity with the same code. Passed Activity context in the Adapter constructor. Then called the Method as follows from button click event.

((MyActivity)context).makePhoneCall("1234567890");

This did the trick. But i feel there should be a better method of doing this. As i am primarily from C background, i am not sure. So my question is there a better or more proper way to do this , according to Java programming conventions.

3 Answers
Related