Я создал базу данных комнат. Здесь весь необходимый код
Dao:
@Dao
public interface LessonDao {
@Update
void update(Lesson...lesson);
@Query("DELETE FROM lesson_table")
void deleteAllLessons();
@Insert
void insert(Lesson lesson);
@Query("SELECT * FROM lesson_table WHERE day LIKE :daycount ORDER BY lesson DESC")
LiveData<List<Lesson>> getLessonsByDay(int daycount);
}
Репозиторий:
public class LessonRepository {
private LessonDao lessonDao;
private LiveData<List<Lesson>> lessonsByDay;
public LessonRepository(Application application){
LessonDatabase database = LessonDatabase.getInstance(application);
lessonDao = database.lessonDao();
}
public void insert(Lesson...lessons){
App.executorService.execute(() -> {
lessonDao.insert(lessons[0]);
});
}
public void update(Lesson...lessons){
App.executorService.execute(()->{
lessonDao.update(lessons[0]);
});
}
public void deleteAllLessons(){
App.executorService.execute(()->{
lessonDao.deleteAllLessons();
});
}
public LiveData<List<Lesson>> getLessonsByDay(int daycount){
return lessonDao.getLessonsByDay(daycount);
}
public LiveData<List<Lesson>> getAllLessons(){
return lessonDao.getAllLessons();
}
}
ViewModel:
public class LessonViewModel extends AndroidViewModel {
private LessonRepository repository;
private LiveData<List<Lesson>> lessonsByDay;
public LessonViewModel(@NonNull Application application) {
super(application);
repository = new LessonRepository(application);
}
public void insert(Lesson lesson){
repository.insert(lesson);
}
public void update(Lesson...lesson){
repository.update(lesson);
}
public void deleteAllLessons(){
repository.deleteAllLessons();
}
public LiveData<List<Lesson>> getLessonsByDay(int daycount){
lessonsByDay = repository.getLessonsByDay(daycount);
return lessonsByDay;
}
public LiveData<List<Lesson>> getAllLessons(){
return repository.getAllLessons();
}
}
Это код в MainActivity для запуска второе действие для редактирования
Intent editLesson = new Intent(MainActivity.this, editLesson.class);
editLesson.putExtra("start time", lesson.getStartTime())
.putExtra("end time", lesson.getEndTime())
.putExtra("subject", lesson.getSubject())
.putExtra("room", lesson.getRoom())
.putExtra("teacher", lesson.getTeacher())
.putExtra("id", lesson.getId())
.putExtra("day", lesson.getDay())
.putExtra("lesson", lesson.getLesson());
startActivityForResult(editLesson, EDIT_LESSON);
Это onActivityResult:
if (resultCode == RESULT_OK && requestCode == EDIT_LESSON) {
String startTime = data.getStringExtra("start time");
String endTime = data.getStringExtra("end time");
String subject = data.getStringExtra("subject");
String room = data.getStringExtra("room");
String teacher = data.getStringExtra("teacher");
int lessonno = data.getIntExtra("lesson", 0);
int day = data.getIntExtra("day", 0);
int id = data.getIntExtra("id", -1);
if(id != -1){
Lesson lesson = new Lesson(subject, room, teacher, startTime, endTime, lessonno, day);
lesson.setId(id);
lessonViewModel.update(lesson);
adapter.notifyDataSetChanged();
Toast.makeText(this, "Lesson updated", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Lesson could not be updated", Toast.LENGTH_SHORT).show();
}
}
И, наконец, это действие, которое вызывается и в котором вы можете редактировать данные
Intent data = new Intent();
data.putExtra("start time", startTime.getText().toString())
.putExtra("end time", endTime.getText().toString())
.putExtra("subject", subject.getText().toString())
.putExtra("room", room.getText().toString())
.putExtra("teacher", teacher.getText().toString())
.putExtra("lesson", getIntent().getIntExtra("lesson", 0))
.putExtra("day", getIntent().getIntExtra("day", 0));
int id = getIntent().getIntExtra("id", -1);
if (id != -1){
data.putExtra("id", id);
}
setResult(RESULT_OK, data);
finish();
Кто-нибудь знает, почему LiveData пуста, когда я возвращаюсь в MainActivity?