I have a problem trying to update a ListView in a fragment which is inside a ViewPager fragment. The whole scenario is that a FragmentActivity works like the base to communicate between fragments.
Specifically, a ListFragment single choice fragment sends an object reference in FragmentActivity and then this redirected to the ViewPager fragment which in turn should update another Fragment child of it.
Everything seemd to operate perfect and the ViewPager fragment the first time being created shows the data I need, BUT, then when you select a row in the ListFragment that initiates this procedure while gets into the ViewPager fragment in this block of code,
public void refreshDataWithClassType(ClassType classType) {
Log.d(TAG, "Should refresh data");
FragmentPagerAdapter fragmentPagerAdapter = (FragmentPagerAdapter) viewPager.getAdapter();
for(int i = 0; i < fragmentPagerAdapter.getCount(); i++) {
ExamsListFragment viewPagerFragment = (ExamsListFragment) fragmentPagerAdapter.getItem(i);
if(viewPagerFragment != null) {
// Do something with your Fragment
// Check viewPagerFragment.isResumed() if you intend on interacting with any views.
viewPagerFragment.refreshExams(classType.getClassTypeId());
}
}
}
I just send the message to every ViewPagerFragment to refresh my data (Exams). But after some time while I got NullPointerException I found out that in the refreshExams method,
public void refreshExams(int classTypeId) {
Bundle args = new Bundle();
args.putInt(KEY_POSITION, getArguments().getInt(KEY_POSITION));
args.putInt(KEY_CLASS_TYPE, classTypeId);
setArguments(args);
getRepositoryExams();
if (examsArrayAdapter == null) Log.d(TAG, "ExamsArrayAdapter is NULL");
if (getActivity() == null) Log.d(TAG, "Activity is NULL");
if (examsList.size() == 0) {
examsArrayAdapter.clear();
examsArrayAdapter.notifyDataSetChanged();
} else {
examsArrayAdapter = new ExamsArrayAdapter(getActivity(), R.layout.exam_row_layout, (ArrayList<Exam>) examsList);
listView.setAdapter(examsArrayAdapter);
}
}
now even I already supposed to have this instance of Fragment and it's OK the first time that it is beign created,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
rootView = inflater.inflate(R.layout.exams_list_fragment, container, false);
listView = (ListView) rootView.findViewById(R.id.examsListView);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
callBackExamListener = (OnExamSelectedListener) activity;
} catch (ClassCastException ex) {
throw new ClassCastException(activity.toString() + " must implement OnExamSelectedListener");
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getRepositoryExams();
examsArrayAdapter = new ExamsArrayAdapter(getActivity(), R.layout.exam_row_layout, (ArrayList<Exam>) examsList);
listView.setAdapter(examsArrayAdapter);
listView.setOnItemClickListener(listRowItemClickListener);
}
getActivity() and examsArrayAdapter is NULL ????
How to fix this?
Thank you in advance!