Reputation: 1266
I have a ViewPager within a Fragment in my application. Within the ViewPager I have 6 list views which pull their data from a central data source (a Schedule, which each list view then pulls Lists of days from). Now I'm trying to update this schedule object by changing the schedule that is stored in my PagerAdapter class, and then updating the ArrayAdapter that serves as the adapter to all the list views. When one list view is switched to, a new array adapter is created with the correct data.
Unfortunately this doesn't work at all. I cannot see any data at any point in the application lifecycle. So I'm assuming I'm doing something fundamentally wrong in connecting my data to my ViewPager...
I've been all over the net, read up on all of the fragment stuff, a LOT of the view pager stuff... Does anyone know the correct way to do this?
Heres the code for my 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;
}
}
Upvotes: 3
Views: 1595
Reputation: 87064
If that is the full code for the instantiateItem
method then it's normal that you don't see any data in the lists. You inflate the layout file named R.layout.schedule_list_view
(in which you, most likely, have a ListView
) and in the same instantiateItem
method you create an independent ListView
widget on which you set the data. As the data is binded to the ListView
which is not in the layout file, you don't see anything as the original ListView
remains empty.
The solution is to either set the data on the ListView
that is in the inflated layout(R.layout.schedule_list_view
) if you have it there, or add the created ListView
with data to the inflate layout(layout
(this is a View
, so you would want to cast it to a ViewGroup
or a subclass of ViewGroup
)).
Upvotes: 3
Reputation: 14520
Instead of creating a new adapter with new data, try to change the data in the existing adapter.
Hope this will work...:)
Upvotes: 0