java-在ViewPager中更新列表视图的正确方法?
作者:互联网
我的应用程序中的片段中有一个ViewPager.在ViewPager中,我有6个列表视图,它们从中央数据源(一个Schedule)中提取其数据,然后从每个列表视图中提取“天数列表”.现在,我试图通过更改存储在我的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;
}
}
解决方法:
如果这是InstantiateItem方法的完整代码,则通常在列表中看不到任何数据.您将名为R.layout.schedule_list_view的布局文件充气(最有可能在其中拥有一个ListView),并在相同的InstantIateItem方法中创建一个独立的ListView小部件来设置数据.由于数据绑定到不在布局文件中的ListView,因此您看不到任何内容,因为原始ListView保持为空.
解决方案是要么在充气布局(R.layout.schedule_list_view)中的ListView上设置数据(如果有),要么将创建的带有数据的ListView添加到充气布局(layout(这是一个View,所以您可以将其强制转换为ViewGroup或ViewGroup的子类).
标签:viewpagerindicator,android-fragments,android-viewpager,java,android 来源: https://codeday.me/bug/20191127/2076174.html