Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulating an Android ViewPager with dynamic data
    primarykey
    data
    text
    <p>I've been racking my brain with this most of the night. I have tried countless (unsuccessful) attempts to resolve it myself.</p> <p>Below is my full code. Additionally, it can be found on <a href="http://wdtr.kr/r9a2a" rel="nofollow noreferrer">Github</a>.</p> <p>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.</p> <pre><code>public class MainActivity extends FragmentActivity { ArrayList&lt;DataSummary&gt; 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&lt;History&gt; 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; } } } </code></pre> <p>Here are some screen shots. <img src="https://i.stack.imgur.com/MGJEe.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/HDcUd.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/J0CYF.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/xylNO.png" alt="enter image description here"></p> <p>Edit (in response to @dymmeh): Here is a screenshot of the values in the summary <code>ArrayList</code> <img src="https://i.stack.imgur.com/XJXqS.png" alt="summary arraylist"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload