I'm trying to customize a ParseQueryAdapter and implement caching. I try to cache results in the onLoaded method of the adapter, but the problem is, neither onLoading nor onLoaded gets called at all as seen in the Logcat. When trying to implement those two methods directly in the actual Activity they do get called, but not when implementing them in this separate adapter class.
This is my code:
public class SongbookAdapter extends ParseQueryAdapter<Song> implements ParseQueryAdapter.OnQueryLoadListener<Song> {
private static String USERNAME = "username";
private static String PIN_LABEL_SONGS = "songs";
public Context context;
public SongbookAdapter(final Context context, final ParseUser organizer) {
super(context, new ParseQueryAdapter.QueryFactory<Song>() {
public ParseQuery<Song> create() {
ParseQuery<Song> query = ParseQuery.getQuery(Song.class);
query.whereEqualTo(USERNAME, organizer.get(USERNAME));
// If internet is down, we query from the cache in the local datastore
if(!Methods.isOnline(context)) {
query.fromPin(PIN_LABEL_SONGS);
}
return query;
}
});
this.context = context;
Log.i("SongbookAdapter", "SongbookAdapter was created");
}
@Override
public View getItemView(Song song, View v, ViewGroup parent) {
...
}
@Override
public void onLoading() {
Log.i("SongbookAdapter", "OnLoading was called");
}
@Override
public void onLoaded(final List<Song> songs, Exception e) {
Log.i("SongbookAdapter", "OnLoaded was called");
if(e == null) {
Log.i("SongbookAdapter", "Loading songs in adapter was successful");
// If we have internet connection, we cache the results in the local datastore
if(Methods.isOnline(context)) {
cacheResults(songs);
}
} else {
// Something went wrong
Log.e("SongbookAdapter", "Loading songs in adapter failed");
e.printStackTrace();
}
}
Logcat:
W/KeyCharacterMap﹕ No keyboard for id -1
W/KeyCharacterMap﹕ Using default keymap: /system/usr/keychars/qwerty.kcm.bin
D/SongbookAdapter﹕ SongbookAdapter was created
D/dalvikvm﹕ GC_FOR_MALLOC freed 1074K, 47% free 4858K/9095K, external 3496K/4366K, paused 20ms
I/dalvikvm-heap﹕ Grow heap (frag case) to 11.465MB for 844153-byte allocation
D/dalvikvm﹕ GC_FOR_MALLOC freed <1K, 43% free 5682K/9927K, external 3496K/4366K, paused 60ms
D/dalvikvm﹕ GC_FOR_MALLOC freed 1491K, 48% free 5166K/9927K, external 3119K/3895K, paused 15ms
D/dalvikvm﹕ GC_FOR_MALLOC freed 515K, 43% free 5675K/9927K, external 3119K/3895K, paused 16ms
I/dalvikvm-heap﹕ Grow heap (frag case) to 11.895MB for 844153-byte allocation
D/dalvikvm﹕ GC_FOR_MALLOC freed 0K, 40% free 6500K/10759K, external 3119K/3895K, paused 15ms
D/dalvikvm﹕ GC_EXTERNAL_ALLOC freed 1017K, 50% free 5483K/10759K, external 3119K/3895K, paused 22ms
D/szipinf﹕ Initializing inflate state
Am I missing something? Why don't onLoading and onLoaded get called at all?
Appreciate any help!
Found the answer to my question - I had forgotten to add the onQueryLoadListener in the constructor. The correct constructor is therefore: