我的应用程序的 Fragment 中有一个 ViewPager。在 ViewPager 中,我有 6 个列表视图,它们从中央数据源(一个时间表,然后每个列表视图从中提取天数列表)提取数据。现在我试图通过更改存储在我的 PagerAdapter 类中的计划来更新此计划对象,然后更新用作所有列表视图的适配器的 ArrayAdapter。当切换到一个列表视图时,将使用正确的数据创建一个新的数组适配器。
不幸的是,这根本不起作用。我在应用程序生命周期的任何时候都看不到任何数据。所以我假设我在将我的数据连接到我的 ViewPager 时做错了什么......
我一直在网上,阅读所有片段的东西,很多视图寻呼机的东西......有人知道正确的方法吗?
这是我的 DayAdapter (ViewPagerADapter) 的代码
public final class DayAdapter extends PagerAdapter {
//~Data Fields----------------------------------------//
/**
* The list adapter that the current list is being handed to.
*/
private Schedule schedule;
/**
* The current index of the day that is to be displayed.
*/
private int currentIndex;
/**
* ArrayAdpter to be used to connect data to all of the list views.
*/
private ArrayAdapter<Course> adapter;
//~Constructors---------------------------------------//
/**
* Constructor for the DayAdapter implementation of PagerAdapter. Takes in a
* Schedule object which is to be the data backing of the views displayed.
*
* @param theSChedule the Schedule object that is the backing for the ListViews.
*/
public DayAdapter(Schedule theSchedule) {
schedule = theSchedule;
currentIndex = schedule.getTodayIndex();
}
//~Methods--------------------------------------------//
public void setSchedule(Schedule theSchedule) {
schedule = theSchedule;
adapter.clear();
adapter.add(schedule.getDay(currentIndex).getList().get(0));
adapter.notifyDataSetChanged();
}
@Override
public Object instantiateItem(ViewGroup container, int index) {
LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.schedule_list_view, null);
ListView view = new ListView(container.getContext());
adapter = new ArrayAdapter<Course>(container.getContext(),
R.layout.list_view_child, schedule.getDay(index).getList());
view.setAdapter(adapter);
//TEST CODE!!!! This does not yield any sort data items in the list views!?
adapter.add(new Course("test", "test", "test", "test", "test", "test", "test"));
currentIndex = index;
((ViewPager) container).addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager pager = (ViewPager) container;
View view = (View) object;
pager.removeView(view);
}
/**
* Gets the pageTitle, which is the name of the day that is at position.
*
* @param position the index of the day selected.
* @return the name of the day the index refers to.
*/
@Override
public CharSequence getPageTitle(int position) {
return schedule.getDay(position).getThisDay();
}
@Override
public int getCount() {
return 6;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}