我有以下類擴展片段:如何更新Fragment UI中的數據?
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
private GridView events;
public DummySectionFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
switch (args.getInt(ARG_SECTION_NUMBER)) {
case 1 : return initializeCalendarScreen(inflater);
case 2 : return inflater.inflate(R.layout.todo_lists_layout, null);
}
return null;
}
public void updateGridView() {
int daysInCurrentMonth=(MainActivity.month==1) ? daysInFebruary : COUNT_OF_DAYS_IN_MONTH[MainActivity.month];
int daysInPrevMonth=(MainActivity.month-1==1) ? daysInFebruary : COUNT_OF_DAYS_IN_MONTH[MainActivity.month-1];
int daysInNextMonth=(MainActivity.month+1==1) ? daysInFebruary : COUNT_OF_DAYS_IN_MONTH[MainActivity.month+1];
Calendar calendar=Calendar.getInstance();
calendar.set(MainActivity.year, MainActivity.month, 1);
int startDayOfWeek=calendar.get(Calendar.DAY_OF_WEEK);
List<Integer> dates=new ArrayList<Integer>();
for (int i=0; i<startDayOfWeek-1; i++) {
dates.add(daysInPrevMonth-i);
}
for (int i=0; i<daysInCurrentMonth; i++) {
dates.add(i+1);
}
ArrayAdapter<Integer> adapter=new ArrayAdapter<Integer>(getActivity(), R.layout.grid_view_cell_layout, R.id.textViewGridCell, dates);
events.setAdapter(adapter);
}
private View initializeCalendarScreen(LayoutInflater inflater) {
View layout=inflater.inflate(R.layout.calendar_layout, null);
events=(GridView)layout.findViewById(R.id.gridViewCalendar);
updateGridView();
return layout;
}
}
它的工作好(我用它FragmentPagerAdapter)。我使用Activity中的方法updateGridView()來更新GridView中的值。但是,如果我這樣做,我會得到NullPointerException,因爲ArrayAdapter的getActivity()返回null。請告訴我,如何從Activity更新Fragment UI?謝謝