0

I've been racking my brain with this most of the night. I have tried countless (unsuccessful) attempts to resolve it myself.

Below is my full code. Additionally, it can be found on Github.

When I run the app, the data displayed is rarely for the month that is showing in the title bar or in the (temporary) TextViews I put on the screen (to ensure I was getting the correct day/month I was expecting.

public class MainActivity extends FragmentActivity {
    ArrayList<DataSummary> summary;
    int mMonth = 0, mYear = 0;
    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link android.support.v4.app.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}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    static DbHelper mDbHelper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDbHelper = DbHelper.getInstance(this);

        summary = mDbHelper.getDataSummary(DbHelper.TABLE_HISTORY);

        setContentView(R.layout.activity_main);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

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

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the primary sections of the app.
     */
    public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            DataSummary sumItem = summary.get(i);
            args.putString(DummySectionFragment.YEAR, sumItem.getYear());
            args.putString(DummySectionFragment.MONTH, sumItem.getMonth());
            args.putString(DummySectionFragment.COUNT, sumItem.getCount());
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            return summary.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            DataSummary sumItem = summary.get(position);
            String monthName = Utility.getMonth(Integer.valueOf(sumItem.getMonth())).toUpperCase();
            CharSequence pageTitle = monthName + " " + sumItem.getYear() + "(" + sumItem.getCount() + ")";
            return pageTitle;
        }
    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {
        public static final String YEAR = "year";
        public static final String MONTH = "month";
        public static final String COUNT = "count";

        public DummySectionFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Bundle args = getArguments();
            String strYear = args.getString(YEAR);
            String strMonth = args.getString(MONTH);
            Integer iMonth = Integer.valueOf(strMonth);
            ArrayList<History> wods = mDbHelper.getHistoryByMonth(strYear, 
                    Utility.pad(iMonth), Constants.SORT_BY_DATE);

            View mFragmentView = inflater.inflate(R.layout.wod_history, null);
            TextView tvMonth = (TextView) mFragmentView.findViewById(R.id.wodHistoryMonth);
            TextView tvYear = (TextView) mFragmentView.findViewById(R.id.wodHistoryYear);
            ListView listHistory = (ListView) mFragmentView.findViewById(R.id.listHistory);

            tvMonth.setText(strMonth);
            tvYear.setText(strYear);
            CustomHistoryView chv = new CustomHistoryView(container.getContext(), wods);
            listHistory.setAdapter(chv);
            return mFragmentView;
        }
    }
}

Here are some screen shots. enter image description here enter image description here enter image description here enter image description here

Edit (in response to @dymmeh): Here is a screenshot of the values in the summary ArrayList summary arraylist

acedanger
  • 1,197
  • 2
  • 15
  • 34
  • 1
    A quick glance everything looks fine. Are you sure you're populating your DataSummary items properly in mDbHelper.getDataSummary(DbHelper.TABLE_HISTORY); ? – dymmeh Sep 13 '12 at 01:11
  • I'm going to dig into it more. It's really perplexing to me. I have a bit more debugging I need to do with this. It really is driving me crazy though. It seems like it should work the way it is. I'm hoping it's something simple like you're asking. Thanks for reviewing/validating my code. – acedanger Sep 13 '12 at 13:05
  • I added a screen shot of my summary `ArrayList` from debugging. This is corresponds to the data I have loaded in the database. – acedanger Sep 13 '12 at 13:52
  • What about mDbHelper.getHistoryByMonth(strYear, Utility.pad(iMonth), Constants.SORT_BY_DATE); ? that must be retrieving something wrong, then.. that seems to be the only other possible culprit – dymmeh Sep 13 '12 at 15:07
  • I've debugged that many times. I know it's grabbing the correct data for the year/month passed. It's as if the data displayed is for the 'next' fragment and not for the current fragment. – acedanger Sep 13 '12 at 15:19
  • Strange. Maybe there's something weird with persisting your fragment's state that's happening in the FragmentStatePagerAdapter. Have you tried using a FragmentPagerAdapter and just reloading all of the data to see if thats the problem? Does your first fragment always load properly? – dymmeh Sep 13 '12 at 15:39
  • No, the first fragment shows what should be displayed the 2nd screen, i.e. the September screen is showing August data. – acedanger Sep 14 '12 at 16:54
  • I am also getting the same kind of problem -- [myIssue](http://stackoverflow.com/questions/14888608/replacing-a-fragment-from-viewpager-android) – Aashutosh Sharma Feb 15 '13 at 05:17
  • I still have not figured this out. I'm stuck on it and just moved on. – acedanger Feb 15 '13 at 11:45

0 Answers0