How to start Activity from Fragment if CrimeAdapter is a separate Class


#1

Hello All,
I have put my RecyclerAdapter Code along with ViewHolder Class in a separate java class and not inside the CrimeListFragment .

However I am not able to call CrimeActivity from inside the adapter, Could anyone suggest a way to call activity from fragment if i keep the adapter code separate and not inside CrimeListFragment?

Many Thanks
Henna


#2

You’ll have to pass in a Context to your adapter. I’d suggest adding a variable to the constructor of your adapter that takes in that Context object (which is your Activity) and uses it to start the next activity.


#3

Thanks @cstewart for your reply :smiley:. I see this is an easier way than what i have implemented in my code.
what I have done is created an onClickListener interface in Adapter and made my fragment implement that interface passing in the adapter position.

Regards


#4

Yea, a callback is a good option as well. Both of these solutions are pretty common.


#5

Hello @cstewart,
I am stuck on 2 pane layouts for this same query. How do I refer to callbacks interface if my RecyclerView is a separate class

EDIT - I managed to do so, I passed in the callback when creating Adapter and called the method in ViewHolder.

Different Issue - I am implementing the logic in a different way using three fragments, and somehow my alias dont work in setContentView(R.layout.masterDetail), it gives me resource not found error
I think there is a different way to use alias here, not sure how. Could you give some guidance on it

EDIT:- above issue also solved, i was using wrong layout file name :smile:

Many Thanks