, когда моя база данных пуста, я получаю данные из веб-сервиса и пытаюсь снова извлечь их из базы данных, чтобы показать их в программе recyclerview, но извлекаю данные из таблицы, которые работают только с новым экземпляром операции или фрагмента и не работают после получения данных извеб-сервис например:
public class FragmentSectionsList extends Fragment {
@Inject
transient JobManager taskManager;
@BindView(R.id.month_sections_list)
RecyclerView month_sections_list;
private Unbinder unbinder;
private Activity activity;
private Context context;
private List<MonthSections> monthSectionsItems = new ArrayList<>();
private SectionsListAdapter adapter;
private ApplicationComponent component;
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
if (getArguments() != null) {
}
component = DaggerApplicationComponent.builder()
.instagramApplicationComponent(CoreApplication.getComponent())
.build();
component.inject(this);
EventBus.getDefault().register(this);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sections_list, container, false);
unbinder = ButterKnife.bind(this, view);
activity = getActivity();
context = getContext();
Delete.tables(MonthSections.class, SectionLesson.class);
FlowManager.getDatabase(AppDatabase.NAME).reset();
/* below code work fine when database is not empty and can be return relation ship */
monthSectionsItems = SQLite.select().from(MonthSections.class).queryList();
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
month_sections_list.setLayoutManager(mLayoutManager);
month_sections_list.setItemAnimator(new DefaultItemAnimator());
month_sections_list.setAdapter(adapter);
if (monthSectionsItems.size() == 0) {
...
taskManager.addJobInBackground(new GetAllMonthSection(UUID.randomUUID().toString()));
}
return view;
}
@Override
public void onDestroyView() {
unbinder.unbind();
super.onDestroyView();
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(GetAllMonthSection.EventGetAllMonthSection event) {
if (Alerter.isShowing()) {
Alerter.hide();
}
if (event.isState()) {
try {
for (int index = 0; index < event.getData().length(); index++) {
JSONObject month = event.getData().getJSONObject(index);
MonthSections monthSections = new MonthSections();
monthSections.setSection_month_name(month.getString("section_month_name"));
monthSections.setSection_price(month.getString("section_price"));
monthSections.setSection_available(month.getBoolean("section_available"));
monthSections.setLessons_count(month.getInt("lessons_count"));
monthSections.setMonth_title(month.getString("month_title"));
monthSections.save();
MonthSections latestId = SQLite.select().from(MonthSections.class).orderBy(MonthSections_Table.id, false).querySingle();
JSONArray section_lesson = month.getJSONArray("section_lessons");
for (int lessonIndex = 0; lessonIndex < section_lesson.length(); lessonIndex++) {
JSONObject lesson = section_lesson.getJSONObject(lessonIndex);
SectionLesson sectionLesson = new SectionLesson();
sectionLesson.setMonthId(latestId.getId());
sectionLesson.setTitle(lesson.getString("title"));
sectionLesson.setContent(lesson.getString("content"));
sectionLesson.setFile_url(lesson.getString("file_url"));
sectionLesson.setTime(lesson.getString("time"));
sectionLesson.setMedia(lesson.getString("media"));
sectionLesson.setCourse(lesson.getString("course"));
sectionLesson.save();
}
}
updateAdapter();
...
} catch (JSONException e) {
e.printStackTrace();
}
} else {
}
}
private void updateAdapter() {
/* below code dont work after save data into database and return empty relation ship */
monthSectionsItems = new ArrayList<>();
monthSectionsItems = SQLite.select().from(MonthSections.class).queryList();
adapter.setData(monthSectionsItems);
}
}