Pravy
Pravy

Reputation: 2205

Access fragment variable from adapter

I have a variable (int variable) in a fragment, I want to access it from adapter(possibly base adapter) set to one of the view( may be list view) of that fragment. How to best access that variable, without making to static.

Upvotes: 1

Views: 5476

Answers (1)

Ken Wolf
Ken Wolf

Reputation: 23269

Store a reference to the parent fragment in the adapter. Set it, for example, in the adapter's constructor.

public MyAdapter (MyFragment fragment) {
    this.fragment = fragment;
}

...

Then later access it in the adapter like this:

int var = fragment.getVariable();

In the fragment make a public method called getVariable().

Upvotes: 4

Related Questions