Моя проблема: я использую Room DB в своем проекте. Когда я нажимаю на одинэлементов в GridView
должно открываться представление списка с данными, что происходит, но элементы списка не меняются.Я получаю данные от Дао на основе позиции.Но элемент в моем наблюдателе LiveData
не получает изменений, он показывает старый список.
Вот мой код:
mGridAdapter = new ProfileGridViewAdapter(mContext, new IProfileGridClickListener() {
@Override
public void cardOnClick(View v, int position) {
// CommonUtilities.showToast("Profile Position" + position, true, mContext);
personalizationQuesEntity = mGridAdapter.getProfileAt(position);
recyclerView_profile.setLayoutManager(new LinearLayoutManager(mContext));
recyclerView_profile.setHasFixedSize(true);
mListAdapter = new ProfileListViewAdapter(mContext, personalizationQuesEntity.questionDataId, new IProfileOptionClickListener() {
@Override
public void listItemOnClick(View v, int position) {
// CommonUtilities.showToast("Profile option Position" + position, true, mContext);
PersonalizationOptionEntity personalizationOptionEntity = mListAdapter.getOptionAt(position);
insertUserResponseInTable(personalizationOptionEntity);
questionsGridView();
}
});
optionList();
recyclerView_profile.setAdapter(mListAdapter);
}
});
questionsGridView();
img_setting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
private void questionsGridView() {
recyclerView_profile.setLayoutManager(new GridLayoutManager(mContext, 4));
recyclerView_profile.setHasFixedSize(true);
recyclerView_profile.setAdapter(mGridAdapter);
ProfileQuesViewModel profileQuesViewModel = ViewModelProviders.of(this).get(ProfileQuesViewModel.class);
profileQuesViewModel.getAllQuestions().observe(this, new Observer<List<PersonalizationQuestionEntity>>() {
@Override
public void onChanged(@Nullable List<PersonalizationQuestionEntity> personalizationQuestionEntities) {
mGridAdapter.setQuestions(personalizationQuestionEntities);
}
});
}
public void optionList() {
ProfileOptionViewModel profileOptionViewModel = ViewModelProviders.of(this, new ProfileOptionVModelFactory(this.getActivity().getApplication(),
personalizationQuesEntity.questionId)).get(ProfileOptionViewModel.class);
profileOptionViewModel.getAllOptions().observe(this, new Observer<List<PersonalizationOptionEntity>>() {
@Override
public void onChanged(@Nullable List<PersonalizationOptionEntity> personalizationOptionEntities) {
mListAdapter.setOptions(personalizationOptionEntities);
}
});
}
Мой вариант ViewModel:
private LiveData<List<PersonalizationOptionEntity>> allOptions;
private ProfileOptionRepository profileOptionRepository;
public ProfileOptionViewModel(@NonNull Application application) {
super(application);
profileOptionRepository = new ProfileOptionRepository(application);
}
public ProfileOptionViewModel(@NonNull Application application, int questionId) {
super(application);
profileOptionRepository = new ProfileOptionRepository(application, questionId);
if (allOptions == null) {
allOptions = new MutableLiveData<>();
}
allOptions = profileOptionRepository.getMyOptions();
}
public LiveData<List<PersonalizationOptionEntity>> getAllOptions() {
return allOptions;
}
public String userImageSelectedOption(String optionText) {
return profileOptionRepository.selectedImgOptionByUser(optionText);
}
Мой Дао:
@Query("select * from personalizationOption WHERE questionId = (:questId) ORDER BY optionId")
LiveData<List<PersonalizationOptionEntity>> getAllOptions(int questId);
Мой репозиторий:
public ProfileOptionRepository(Application application, int questionId) {
database = database.getDatabase(application);
personalizationOptionDao = database.personalizationOptionDao();
allOptions = personalizationOptionDao.getAllOptions(questionId);
}
public LiveData<List<PersonalizationOptionEntity>> getMyOptions() {
return allOptions;
}