Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 房间数据库中的列表始终为空_Android_Android Recyclerview_Android Room_Android Livedata - Fatal编程技术网

Android 房间数据库中的列表始终为空

Android 房间数据库中的列表始终为空,android,android-recyclerview,android-room,android-livedata,Android,Android Recyclerview,Android Room,Android Livedata,以下类用于在房间数据库中存储调查列表。LiveData用于更新 数据库的实体类: @Entity public class Survey { @PrimaryKey @NonNull private String surveyID; private String surveyName; private String url; private double lat; private double lng; private double radius; public Survey(String

以下类用于在房间数据库中存储调查列表。LiveData用于更新

数据库的实体类:

@Entity
public class Survey {

@PrimaryKey
@NonNull
private String surveyID;
private String surveyName;
private String url;
private double lat;
private double lng;
private  double radius;

public Survey(String surveyID, String surveyName, double lat, double lng, double radius, String url) {
    this.surveyID = surveyID;
    this.surveyName = surveyName;
    this.lat = lat;
    this.lng = lng;
    this.radius = radius;
    this.url = url;
}
各自的Dao接口:

@Dao
public interface SurveyDao {

@Query("SELECT * FROM Survey")
LiveData<List<Survey>> getListOfSurveys();

/* Rest omitted */
}
各自的存储库类

public class SurveyRepository {

private final SurveyDao surveyDao;

@Inject
public SurveyRepository (SurveyDao surveyDao){
    this.surveyDao = surveyDao;
}

public LiveData<List<Survey>> getListOfSurveys(){
    return surveyDao.getListOfSurveys();
}
/* Rest omitted */
}
视图模型:

public class SurveyCollectionViewModule extends ViewModel {

private SurveyRepository repository;

public SurveyCollectionViewModule(SurveyRepository repository) {
    this.repository = repository;
}

public LiveData<List<Survey>> getSurveys(){
    return repository.getListOfSurveys();
}
/* Rest Omitted */
}
使用所有这些,我设置了一个片段,其中有一个显示调查列表的RecyclerView。该列表通过以下方式获得:

public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);

    surveyCollectionViewModule = ViewModelProviders.of(this, viewModelFactory)
            .get(SurveyCollectionViewModule.class);

    surveyCollectionViewModule.getSurveys().observe(this, new Observer<List<Survey>>() {
        @Override
        public void onChanged(@Nullable List<Survey> surveys) {
            if(SurveyListFragment.this.listOfSurveys == null) {
                setSurveyData(listOfSurveys);
            }
        }
    });
}

但是,我遇到了一个问题,由于该列表为NULL,适配器中对getItemCount的调用失败。数据库不包含任何条目,但我仍然不明白为什么列表总是返回空值

您的onChanged实际上没有使用给定的surveys参数,请将其更改为调用setSurveyDatasurveys以实际填写数据。

在ViewModel类中进行一些更改,如下所示

    private final LiveData<List<Survey>> SurveyList;

private AppDatabase appDatabase;

public SurveyCollectionViewModule(Application application) {
    appDatabase = AppDatabase.getDatabase(this.getApplication());

    SurveyList = appDatabase.surveyDao().getAllSurvey();
}
public LiveData<List<Survey>> getSurveys() {
    return SurveyList;
}