5

In preference change listener I have set it to download new data using Loader and then fill the ListView, but only the onCreateLoader() function is called when I change the preference, the onLoadFinished() callback function remains uncalled, hence my ListView in not updated with new data, I also happen to have a refresh button that does the same thing i.e. downloads the data gain and fills the list with new data but here the onLoadFinished() is called hence by updating the list.

The refresh button code

case R.id.menu_refresh :
            ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            boolean isConnected = activeNetwork != null &&  activeNetwork.isConnectedOrConnecting();
            if(isConnected)
            {
                getSupportLoaderManager().initLoader(0,null,this).forceLoad();
            }
            else {
                Toast.makeText(this,"No Internet",Toast.LENGTH_SHORT).show();
            }
            break;

The SharedPreference listener code :

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
    ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null &&  activeNetwork.isConnectedOrConnecting();
    if(isConnected)
    {
        //delfromold();
        getSupportLoaderManager().initLoader(0,null,this).forceLoad();
        Log.v("Prefrences Changed",s);
    }
}

EDIT

Loader Implementation

public class Loade extends AsyncTaskLoader<ArrayList<CustomL>>{

Context mCon;

public Loade(Context context) {
    super(context);
    mCon = context;
}

@Override
public ArrayList<CustomL> loadInBackground() {
    ArrayList<CustomL> maList = null;
    try {
        Log.v("Loader","Called");
        maList = GetList.makelist(mCon);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return maList;
}
}

Loader :

public Loader<ArrayList<CustomL>> onCreateLoader(int id, Bundle args) {
    return new Loade(this);
}

public void onLoadFinished(Loader<ArrayList<CustomL>> loader, ArrayList<CustomL> data) {
    Log.v("Load","Finished");
    maklst(data);
}
Nikhil Soni
  • 119
  • 9
  • In the makelist function of the Getlist class the new data gets downloaded and saved into table i can confirm that but, its just stop there the onLoadFinished dosnt get called and maklst function that updates the list with new data is never called but when i call the same loader from refresh the onLoadFinished is called – Nikhil Soni Sep 17 '16 at 06:07

1 Answers1

0

You don't implement and use the Loader properly. It is a hard thing to do, though.

From the code above I see that you use the forceload() method, but you haven't implemented it in your Loader. If you check the source code for Loaders this method just calls the onForceload() event which does nothing. According to docs "Subclasses must implement this to take care of requests to forceLoad(). This will always be called from the process's main thread. "

You should read this tutorial and start from there.

The general concept is that after you finish your AsyncTaskLoader implementation your fragments/activities will not contact your Loader directly. If you do that you will end up having problems with the activity/fragment lifecycle. The LoaderManager is responsible for that using these methods

  1. initLoader
  2. restartLoader
  3. destroyLoader

Afterwards the Loader will use the LoaderManager.LoaderCallbacks interface to report any results back to the fragment/activity.

masp
  • 515
  • 1
  • 5
  • 15