I am trying to implement the viewPager loading the data profile via SQLite after scrolling from one page to another page horizontally. When it comes to the implementation, it shows that there has no data to be load and inserted into the page. After scrolling the other pages, the data has just been erased and not loading the data. My working method to load the data is done at onCreateView. would you please tell me the better way to load the sqlite data in advance?
The below is my code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.list_article);
... mViewPager = (CustomViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setEnabled(true);
mViewPager.setOffscreenPageLimit(1);
mViewPager.setOnPageChangeListener(viewPagerPageChangeListener);
mViewPager.setCurrentItem(0);
OnPageChangeListener viewPagerPageChangeListener = new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
pageId = position + 1;
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
final Animation animationFadeIn = AnimationUtils.loadAnimation(FlightBankActivity.this, R.anim.fade_in);
for (int i = 0; i < dotsCount; i++) {
dots[i].setImageResource(R.drawable.dot_e);
}
dots[position].setAnimation(animationFadeIn);
dots[position].setImageResource(R.drawable.dot_s);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
ArrayList<FlightProfile> fpList = (ArrayList<FlightProfile>) DatabaseManager.getInstance().getAllFlightProfile();
System.out.println(fpList.size() + " : is the size of profile");
System.out.println(pageId+ " : is the pageId");
for(int i = 0 ; i < fpList.size() ; i ++){
System.out.println(fpList.get(i).getId() + " : is the id");
System.out.println( fpList.get(i).getName() + " : is the name");
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
};
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1 , 0);
}
@Override
public int getCount() {
return 7;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
}
return null;
}
}
public static class PlaceholderFragment extends Fragment {
private String tag;
private static int baseNumber = 0;
private static final String ARG_SECTION_NUMBER = "section_number";
public void setCustomTag(String tag)
{
this.tag = tag;
}
public String getCustomTag()
{
return tag;
}
public static PlaceholderFragment newInstance(int sectionNumber, int j) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
args.putInt("type", j);
baseNumber = pageId;
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle=getArguments();
final int type = bundle.getInt("type");
System.out.println("type :" + type);
fp = DatabaseManager.getInstance().getFlightProfile(baseNumber +1);
Aucun commentaire:
Enregistrer un commentaire