我大部分时间一直在用脑子绞尽脑汁。我尝试了无数(不成功)的尝试来解决它。
以下是我的完整代码。此外,它可以在Github上找到。
当我运行应用程序时,显示的数据很少出现在标题栏中显示的月份或我放在屏幕上的(临时)TextView中(以确保我获得了正确的日期/月份)
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;
}
}
}
以下是一些屏幕截图。
编辑(响应@dymmeh):
以下是摘要ArrayList
中值的屏幕截图