I am going through the Android Developer guide on View Paging with fragments and am stuck at populating the fragment from my main onCreate method. I think I am confused as to what methods get called when to populate the fragment as I am getting null values.
My Fragment Class
public class CatalogFragment extends Fragment {
private String filename;
private String author;
private String title;
public CatalogFragment(String f, String t, String a){
filename = f;
author = a;
title = t;
Log.d("frag", f+" | "+t+" | "+a);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.catalog_fragment, container, false);
ImageView imgView = (ImageView) rootView.findViewById(R.id.imgFrag);
Bitmap bmp = BitmapFactory.decodeFile(filename);
imgView.setImageBitmap(bmp);
TextView txtView = (TextView)rootView.findViewById(R.id.title_text);
txtView.setText(author+": "+title);
return rootView;
}
}
My onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog_view);
//Instantiate a ViewPager and a PagerAdapter
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
//populate the fragment
//I was thinking I could pass the fragment class the necessary information to put an imageview and text view in place.
List<Poster> posters = db.getAllPosters();
for (Poster p : posters) {
final String filename = p.getPosterFilename();
//Create a new Fragment?
new CatalogFragment(filename, p.getPosterTitle(), p.getPresenterFname()+" "+p.getPresenterLname());
}
}
However it appears that getItem is what is actually being called to populate the fragment. I think. If I put null in for the arguments that is what I get. If I try to omit this line eclipse starts to complatin. So I am stuck.
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int position){
return new CatalogFragment(null, null, null);
}
@Override
public int getCount(){
return NUM_PAGES;
}
}