2

So to start with I pinched the code from here, I have set the page titles to display the date, which works well, but I am having trouble with the positions and what Date is actually being sent over in the Bundle, it starts off with it being todays date and the position being 2000,

I swipe right and the position and date go to 2002 and 2 days after today, but the PagerTitleStrip will be correct and say its tomorrow.

The same happens if I swipe left, apart from its decremented.

So my question is, "Do you know why this strange behaviour is happening and how can I fix it?"

So here is the code, am I doing something wrong?

Also, when the app loads up to the ViewPager screen, the PagerTitleStrip will be as follows (I think this is what is causing the issue)

[yesterdays date] [todays date] [tomorrows date]

public class MainMenuActivity extends AppCompatActivity {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link FragmentPagerAdapter} derivative, which will keep every
 * loaded fragment in memory. If this becomes too memory intensive, it
 * may be best to switch to a
 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
 */
BootstrapPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;
ArrayList<Set> selected = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_menu_setup);
    try {
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setLogo(R.mipmap.ic_launcher);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        getSupportActionBar().setTitle("ProjectME");
    } catch(NullPointerException n) {
        Log.v("nullPointerCaught", n.toString());
    }


    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new BootstrapPagerAdapter(getResources(), getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setCurrentItem(2000);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    if(id == R.id.action_add) {
        Intent intent = new Intent(MainMenuActivity.this, CategoryListView.class);
        startActivity(intent);
    }

    if(id == R.id.action_cal) {
        Intent intent = new Intent(MainMenuActivity.this, CalendarView.class);
        startActivity(intent);
    }

    if(id == R.id.action_copy) {
        Toast.makeText(getApplicationContext(), "Copy has been clicked", Toast.LENGTH_SHORT).show();
    }

    return super.onOptionsItemSelected(item);
}

public ArrayList<Set> getSelected() {
    return selected;
}

public void setSelected(ArrayList<Set> selected) {
    this.selected = selected;
}

public class BootstrapPagerAdapter extends FragmentPagerAdapter  {

    /**
     * Create pager adapter
     *
     * @param resources
     * @param fragmentManager
     */

    public BootstrapPagerAdapter(Resources resources, FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public int getCount() {
        return 10000;
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        DateTime pagerdate = DateTime.now(TimeZone.getDefault());
        DateTime days = pagerdate.plusDays(position - 2000);
        return days.format("DD/MM/YYYY").toString();
    }

    @Override
    public Fragment getItem(int position) {


        DateTime pagerdate = DateTime.now(TimeZone.getDefault());
        DateTime days = pagerdate.plusDays(position - 2000);

        Bundle bundle = new Bundle();
        bundle.putString("date", days.format("DD/MM/YYYY").toString());
        bundle.putInt("position", position);
        Log.v("date", days.format("DD/MM/YYYY").toString());
        Log.v("position", position + "");
        MainMenuView2 mainMenuView2 = new MainMenuView2();
        mainMenuView2.setArguments(bundle);
        return mainMenuView2;
    }
}
}
Community
  • 1
  • 1
Joeliomason
  • 139
  • 2
  • 15
  • to start off, for "infinite" things you have to use `FragmentStatePagerAdapter` instead of `FragmentPagerAdapter` – injecteer Apr 22 '15 at 12:38
  • just added 'FragmentStatePagerAdapter' i'll look through the developer documentation and see what I can find – Joeliomason Apr 22 '15 at 12:43
  • I don't get, what you mean with "strange behaviour" – injecteer Apr 22 '15 at 12:49
  • it "skips over" 1999 and 2001 (positions, not years) so it is 1 day behind – Joeliomason Apr 22 '15 at 13:04
  • "I swipe right and the position and date go to 2002 and 2 days after today" where do you see it? in log? – injecteer Apr 22 '15 at 13:08
  • very sorry, yes its in Log, but the PagerTitleStrip is displaying what it should display – Joeliomason Apr 22 '15 at 13:14
  • well, if it's in the log only, it's fine. The pageAdapter is initializing apart from the current "visible" fragment, the "not-yet-visible" position-1 and position+1 fragments. What you get to see in the log – injecteer Apr 22 '15 at 13:17
  • so when I come to compare the date of the "page" its on now to a date stored in a database, it will be fine and compare the date of the currently selected page? – Joeliomason Apr 22 '15 at 13:18
  • errrmm, yes? sorry, I have no clue what you want to say. %) – injecteer Apr 22 '15 at 13:22
  • okay, so say im on page 2000, where the date is todays date (22/04/2015). I will be comparing todays date with some values in my database, so I can get the data I want thats relevant to the date im giving it, so even though the log is messing up, will it be giving the database the correct date (page) im on, or will it be giving it what the log is showing? – Joeliomason Apr 22 '15 at 13:26
  • yes, each fragment instance gets it's own CORRECT date (via `setArguments(bundle)`). – injecteer Apr 22 '15 at 13:38
  • btw, if you pull the data from the DB inside each fragment, it's simpler and more efficient to use a `FragmentStatePagerAdapter` backed by a `cursor` – injecteer Apr 22 '15 at 13:40
  • would you be able to provide me a link where this has been used?? I am new to Fragments thats all :) – Joeliomason Apr 22 '15 at 13:45
  • see my answer with an example – injecteer Apr 22 '15 at 13:54

1 Answers1

0

the Cursor-based Adapter implementation can look like this:

class SwipeAdapter extends FragmentStatePagerAdapter  {

  private final Cursor cursor;

  private final int count;

  public SwipeAdapter( FragmentManager fm ) {
    super( fm );
    cursor = initCursorSomehow();
    count = cursor.getCount();
  }

  @Override
  public int getCount() {
    return count;
  }

  @Override
  public CharSequence getPageTitle( int position ) {
    cursor.moveToPosition( position );
    return cursor.getString( cursor.getColumnIndex( "title" ) );
  }

  @Override
  public Fragment getItem( int position ) {
    Bundle b = new Bundle();
    cursor.moveToPosition( position );
    b.putString( "someDate", cursor.getString( cursor.getColumnIndex( "someDate" ) ) );
    b.putString( "someInt", cursor.getInt( cursor.getColumnIndex( "someInt" ) ) );
    return Fragment.instantiate( getApp(), SomeFragment.class.getName(), b );
  }
}
injecteer
  • 16,220
  • 3
  • 39
  • 72
  • my issue is that when the getItem is called, it creates three fragments, and the last fragment created is to the right, it thinks that the date and position are tomorrows date and starting position +1, is there any way to make it create 1 fragment? – Joeliomason Apr 22 '15 at 15:12
  • no, the pager is always warming up n-1, n and n+1 fragments. The reason: smooth user experience by swiping – injecteer Apr 23 '15 at 10:11