Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Context in FragmentPagerAdapter

I couldn't use context in FragmentPagerAdapter. RadioGroup(this) gives me undefined error. I used getContext() instead of this but couldn't achieve it

private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {  

final RadioGroup rg = new RadioGroup(this);  // what can I use instead of "this" ?

}
like image 444
metemet06 Avatar asked Apr 20 '13 03:04

metemet06


People also ask

How do I use pager adapter fragment?

When using FragmentPagerAdapter the host ViewPager must have a valid ID set. Subclasses only need to implement getItem and getCount to have a working adapter. super. onCreate(savedInstanceState);

How do you call ViewPager fragment from activity?

You'll just need to cast it to the real fragment class if you want to call a specific method. int pos = viewpager. getCurrentItem(); Fragment activeFragment = adapter. getItem(pos); if(pos == 0) ((NPListFragment)activeFragment).

What is Fragment state adapter in Android?

adapter. FragmentStateAdapter instead. Implementation of PagerAdapter that uses a Fragment to manage each page. This class also handles saving and restoring of fragment's state. This version of the pager is more useful when there are a large number of pages, working more like a list view.


1 Answers

I am not sure why you are instantiating a RadioGroup in a FragmentPagerAdapter, but anyway you can get the context by modifying the constructor of the class:

private Context context; 

/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm, Context c) {
    super(fm);
    context = c;
}

Then you can add the context when you create your FragmentPagerAdapter.

like image 167
Yoann Hercouet Avatar answered Nov 15 '22 19:11

Yoann Hercouet