I need to use PackageManager pm = context.getPackageManager();
inside a PagerAdapter but I don't know how to get a context.
public class MyPagerAdapter extends PagerAdapter {
...
}
How can context inside PagerAdapter?
The answer of @DeeV means you must provide a context to your PageAdapter by yourself, either via the constructor or via a setter. Then store it inside the PageAdapter as a field and retrieve it whenever you need it.
private ViewGroup _container;
public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
{
_container = container;
}
private Context GetContext()
{
return (Activity)_container.Context;
}
It's not recommended provide Context, this may cause memory leaks, if you need the context to access resources, you can provide the resources through the constructor.
MyPagerAdapter(val resources: Resources){
}
....
val adapter = MyPagerAdapter(context.resources)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.