Skip to content
Advertisement

From Adapter into Intent

I have an Adapter for a ListView I have. Every item in that list has a Button, when the user presses that Button, it will go into another activity with info about that specific item’s info (info is gotten from it’s associated object).

How can I make it so when I press the Button, I will make a new Intent object, put user.getOfferID() into it and then start that activity? I tried doing the old fashioned way but it doesn’t let me write startActivity(intent);, says it doesn’t exist.

How can I do that?

Here is the Adapter:

JavaScript

Here is the UserItem class:

JavaScript

Advertisement

Answer

startActivity(intent) doesn’t come with free, you need a context. You can call startActivity(intent) inside an ordinary activity class because it has built in context variable. Because you create a new class UsersAdapter, you need to store context that is passed from the constructor by creating a new instance variable.

JavaScript

Then assign it in the constructor.

JavaScript

Now inside the setOnClickListener you can call startActivity using the context and Android Studio won’t show error.

JavaScript
Advertisement