You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Paging Library集成问题:无数据返回求助

Troubleshooting Your Android Paging Library + Room Integration

Hey there, let's figure out why your Paging Library setup isn't returning any data. I’ve gone through your code and spotted several key areas to check:

1. Fix the Syntax Error in Your ViewModel’s init Method

Looking at your ViewModel code, the init method is missing curly braces—this means the code inside won’t execute at all:

// Your current broken code
public void init(String keyword) searchReferencePagedList = new LivePagedListBuilder<>(mRepository.getPagedListSearchResult(keyword), 20).build(); }

// Fixed version with proper syntax
public void init(String keyword) {
    searchReferencePagedList = new LivePagedListBuilder<>(mRepository.getPagedListSearchResult(keyword), 20).build();
}

Without the braces, your searchReferencePagedList never gets initialized, so observing it will always return null.

2. Stop Reinitializing LiveData Repeatedly

Right now, every call to init() creates a brand new LiveData<PagedList<SearchReference>>. When you observe it immediately after calling init, you might run into issues where old observers are stuck on previous LiveData instances, or the new LiveData hasn’t had time to load data.

A cleaner approach uses Transformations.switchMap to handle dynamic search keywords without recreating LiveData:

public class SearchKeywordViewModel extends ViewModel {
    private final SearchKeywordRepository mRepository;
    private final MutableLiveData<String> mKeywordLiveData = new MutableLiveData<>();
    public final LiveData<PagedList<SearchReference>> searchReferencePagedList;

    public SearchKeywordViewModel(SearchKeywordRepository repository) {
        this.mRepository = repository;
        // Auto-switch data source when the keyword changes
        searchReferencePagedList = Transformations.switchMap(mKeywordLiveData, keyword -> 
            new LivePagedListBuilder<>(mRepository.getPagedListSearchResult(keyword), 20).build()
        );
    }

    // Call this to update the search keyword
    public void setSearchKeyword(String keyword) {
        mKeywordLiveData.setValue(keyword);
    }
}

Then update your Fragment code to use this setup:

private void observeKeywordQuery(String keywords) {
    mViewModel.setSearchKeyword(keywords);
    mViewModel.searchReferencePagedList.observe(this, searchReferenceList -> {
        if (searchReferenceList != null && !searchReferenceList.isEmpty()) {
            mRecyclerAdapter.submitList(searchReferenceList);
        } else {
            Log.d(LOG_TAG, "No verses retrieved");
        }
    });
}

This way, you only observe the LiveData once, and it automatically updates whenever the keyword changes.

3. Double-Check Your Room Query’s JOIN Logic

Even though your non-Paging setup worked, there’s a possible typo in your SQL JOIN condition:

INNER JOIN chapters ON chapters.chapter = verses.chapters

Is verses.chapters the correct column name? It’s far more likely the verses table has a singular chapter column that links to chapters.chapter. A typo here would result in an empty result set, which explains why Paging returns no data.

Test the query directly (using Room’s Database Inspector, for example) to confirm it returns rows when run with a valid keyword.

4. Ensure Your RecyclerView Adapter Uses PagedListAdapter

Make sure your adapter extends PagedListAdapter instead of a regular RecyclerView.Adapter—this is required to properly handle PagedList updates:

public class SearchAdapter extends PagedListAdapter<SearchReference, SearchAdapter.ViewHolder> {
    // Constructor and ViewHolder implementation here
}

If you’re using a regular adapter, submitList() won’t work as expected with PagedList data.

5. Confirm Correct Paging + Room Dependencies

Double-check that you’ve added compatible AndroidX dependencies to your module-level build.gradle:

implementation "androidx.paging:paging-runtime:2.1.2" // Use a newer version if available
implementation "androidx.room:room-runtime:2.4.3" // Ensure Room version matches Paging compatibility
annotationProcessor "androidx.room:room-compiler:2.4.3"

Mismatched versions between Room and Paging can cause silent failures.


内容的提问来源于stack exchange,提问作者Daniel Beleza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:25:05