tyczj
tyczj

Reputation: 73753

Refreshing a ListFragment with a LoaderManager

I have a ListFragment class as a subclass of my activity and in my PreferenceActivity I have a setting to change the sorting of 2 out of the 5 possible list views I have but when I return back to the activity with the listviews the onCreateLoader does not get called again so the list remains the same.

How can I update the list after changing the settings value?

an example of my onCreateLoader

if(pref.getString(Preferences.CAL_SORT, "1").equals("1")){
                return new CursorLoader(getActivity(),CalendarEvents.EVENTS_URI,
                        new String[] {CalendarEvents.EVENT_ID,CalendarEvents.EVENT_READ,CalendarEvents.EVENT_SUBJECT,
                            CalendarEvents.EVENT_COMPANY_NAME,CalendarEvents.EVENT_START}
                        ,null,null,CalendarEvents.EVENT_COMPANY_NAME+" COLLATE LOCALIZED ASC," /*+ julianday("*/+CalendarEvents.EVENT_START/* + ")"*/ + " COLLATE LOCALIZED DESC");
            }else{
                return new CursorLoader(getActivity(),CalendarEvents.EVENTS_URI,
                        new String[] {CalendarEvents.EVENT_ID,CalendarEvents.EVENT_READ,CalendarEvents.EVENT_SUBJECT,
                            CalendarEvents.EVENT_COMPANY_NAME,CalendarEvents.EVENT_START}
                        ,null,null,CalendarEvents.EVENT_COMPANY_NAME+" COLLATE LOCALIZED ASC," /*+ julianday("*/+CalendarEvents.EVENT_START/* + ")"*/ + " COLLATE LOCALIZED ASC");
            }

I know I can call notifyDatasetChange() on the adapter but the adapter is not visible to the outer activity. Should I do something in my ContentProvider

This is how my class is structured

public class MainActivity extends FragmentActivity {

    public static class ListViews extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{
        public Loader<Cursor> onCreateLoader(int arg0,Bundle arg1){
        }
    }

    public void onResume(){
    }
}

Upvotes: 2

Views: 4563

Answers (1)

biegleux
biegleux

Reputation: 13247

You have to call getLoaderManager().restartLoader(int, Bundle, LoaderCallbacks). In onCreateLoader() you can create new Loader based on the supplied Bundle.

See sample at http://developer.android.com/reference/android/app/LoaderManager.html how it is used when the filter changes. For detecting preference changes you can use SharedPreferences.OnSharedPreferenceChangeListener.

Upvotes: 5

Related Questions