JavaのPOJOオブジェクト私はsections.getValue()
れをチェックしています次の行でLiveData.getValue()ルーム
public class Section {
@ColumnInfo(name="section_id")
public int mSectionId;
@ColumnInfo(name="section_name")
public String mSectionName;
public int getSectionId() {
return mSectionId;
}
public void setSectionId(int mSectionId) {
this.mSectionId = mSectionId;
}
public String getSectionName() {
return mSectionName;
}
public void setSectionName(String mSectionName) {
this.mSectionName = mSectionName;
}
}
マイQueryメソッド
@Query("SELECT * FROM section")
LiveData<List<Section>> getAllSections();
へのアクセスDB
final LiveData<List<Section>> sections = mDb.sectionDAO().getAllSections();
にnullを返します。私はデータベースにデータを持っていますが、後で私はの値を取得していますが、常に私にnullを与えています方法。
sections.observe(this, new Observer<List<Section>>() {
@Override
public void onChanged(@Nullable List<Section> sections){
}
});
しかし、私が質問からLiveDataを省略すると、私は期待どおりにデータを取得しています。 照会方法:
@Query("SELECT * FROM section")
List<Section> getAllSections();
へのアクセスDB:
final List<Section> sections = mDb.sectionDAO().getAllSections();
なぜ、セクション.getValue()を使用したいのですが、livedataを使用してデータを観察します。次の行にチェックインしているときに、データがlivedataに設定されていないため、nullが返されます。 'LiveData'を使用したくない場合は、' LiveData'なしでそれを使用してください。 – Moinkhan
私はlivedataを使いたい... 'sections.getValue()'がnullの場合データのためにapiを呼び出し、最終的にデータを取得する場所の 'onChange()'メソッドをデータベースに挿入する必要があります。しかし、データベースとAPIの両方からデータを取得して取得すると、その 'ヌル'値が原因です。 – CodeCameo