LS_
LS_

Reputation: 7129

How to pass variable from Activity to Adapter

I need to pass a variable from an Activity to a GridView Adapter. What I tried was:

Declaring a method inside the activity:

public int getPoints()
{
    return points;
}

The in the adapter I declared:

mainActivity sel= new mainActivity();

And finally I called the method using:

int myPoints = sel.getPoints();

But it always returns 0. What is wrong with the code?

Upvotes: 0

Views: 5387

Answers (2)

Vadims Savjolovs
Vadims Savjolovs

Reputation: 2668

  1. Add argument to your adapter constructor
  2. Pass your variable when instantiating adapter MyGridAdapter adapter = new MyGridAdapter(myVariable)

To update variable from your activity you can also create method in your adapter and call it adapter.updateMyVariable(newVariable);

Upvotes: 3

kulvinder
kulvinder

Reputation: 539

Create public static variable points in mainActivity and then call from adapter mainActivity.points.

Upvotes: 0

Related Questions