Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getActivity in Activity

I'm having trouble with getActivity() in Activity. It says can't resolve method getActivity(). I'm using it to make onClick in recyclerView.

Here is my code

 mRecyclerView.setAdapter(mAdapter);
 mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), this));

What should I change getActivity with? Thanks in advance

like image 726
Sekar Anindya Avatar asked Jul 03 '26 10:07

Sekar Anindya


2 Answers

You have declared your code inside of an Activity. Therefore you can simply use this to reference the Activity you are in.

mRecyclerView.setAdapter(mAdapter);
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, this));
like image 137
Newtron Labs Avatar answered Jul 04 '26 22:07

Newtron Labs


change it to

this

this is a reference to the class your in, like MainActivity.this

like image 31
Matthew Shearer Avatar answered Jul 04 '26 23:07

Matthew Shearer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!