I’m working on a home replacement app and I need to get the context to access shared preferences and get the number of screens my app should have to use it on getCount().
I’ve banging my head against the wall for the last few hours but I cannot figure out how to get the context before getCount() is fired.
This is my code:
public class MyPagerAdapter extends PagerAdapter {
public Context context;
public int getCount() {
return 5;
}
@Override
public Object instantiateItem(View container, int position) {
Context context = container.getContext();
}
I’m getting the context on instatiateItem but instatiateItem runs after getCount(), is there a way to get the context directly on the pageAdapter? Or how can I run getContext() before getCount() is fired?
Why dont you pass the
Contextin the constructor of theMyPagerAdapter?Create the constructor of
MyPagerAdapteras follows:And in your activity you should write initialize the PagerAdapter as