android - Android 的 ParseQueryAdapter 不调用其 onLoaded 方法

标签 android parse-platform android-parsequeryadapter

我正在尝试自定义 ParseQueryAdapter 并实现缓存。我尝试在适配器的 onLoaded 方法中缓存结果,但问题是,正如在 Logcat 中看到的那样,根本没有调用 onLoading 和 onLoaded。当尝试在实际 Activity 中直接实现这两个方法时,它们会被调用,但在这个单独的适配器类中实现它们时不会被调用。

这是我的代码:

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();
        }
    }

日志:

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

我错过了什么吗?为什么根本不调用 onLoading 和 onLoaded?

感谢任何帮助!

最佳答案

找到了我的问题的答案 - 我忘记在构造函数中添加 onQueryLoadListener。因此,正确的构造函数是:

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;

    // This it what I forgot
    addOnQueryLoadListener(this);

    Log.i("SongbookAdapter", "SongbookAdapter was created");
}

关于android - Android 的 ParseQueryAdapter 不调用其 onLoaded 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30842340/

相关文章:

android - 如何用jsoup解析lu、li标签?

ios - 在使用 iOS 和 Parse(或任何其他基于云的 SDK)时,我应该添加中间层(如 Java 或 Node)吗?

android - 微调器错误 "Spinner adapter view type count must be 1"

android - 如何在 TabLayout 中增加选项卡的图标大小

android - React Native 中的 RecyclerView : notifyItemInserted() and notifyDataSetChanged() have no effect

arrays - 如何在 tableView 中显示 Parse 中的数组

ios - 如何在objective-c中使用Parse API使用 'AND' 'OR'操作

android - com.parse.ParseRequest$ParseRequestException : bad json response 错误

Android:调用 getExternalFilesDir(null) 时出现 ANR