Fragments in ViewPager all have the data set of the last Fragment
For an App I'm working on, I am trying to implement a ViewPager in which
each Fragment has the same layout consisting of a ListView, but with
different data. I'm trying to do that as follows:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
rootView = inflater.inflate(R.layout.fragment_layout, container, false);
setUpFragmentLayout(rootView, pageNumber);
return rootView;
}
private static void setUpFragmentLayout(View rootView, int pageNumber) {
ListView listView = (ListView) rootView.findViewById(R.id.listView);
setUpListView(listView, pageNumber);
}
private static void setUpListView(ListView listView, int pageNumber) {
...
CustomAdapter customAdapter = new CustomAdapter (MainPager.mContext,
listView.getId(), listOfData);
listView.setAdapter(customAdapter);
}
And in the CustomAdapter class:
private static ArrayList<String> mListOfData;
public CustomAdapter(Context context, int resourceID, ArrayList<String>
listOfData) {
super(context, resourceID, listOfData);
Log.d("Checks", "New CustomAdapter with mListOfData: " + mListOfData +
", and listOfData: " + listOfData);
mListOfData = listOfData;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View listItem;
...
TextView data = (TextView) listItem.findViewById(R.id.data_field);
data.setText(mListOfData.get(position));
return listItem;
}
The problem is that the all Fragments display the data from the last
Fragment because, as I can see in my Log in CustomAdapter, mListOfData is
constantly overwritten (so contrary to what I thought I was doing, I'm
actually not making a new CustomAdapter every time?).
How would I solve this? How do I make each fragment display its own data
set but using still one adapter class?
No comments:
Post a Comment