我知道可以从数据库中获取表中的所有数据,但是我无法获得一种使用外键筛选数据的有效方法。我将如何实现其中的Java部分?
我要做的是在RecyclerView中显示分配给选定术语的所有课程。
/*
* Returns the list of courses assigned to the selected term
*/
RecyclerView recyclerView = findViewById(R.id.term_courses_added_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
AssignCourseAdapter adapter = new AssignCourseAdapter();
recyclerView.setAdapter(adapter);
// courseViewModel = ViewModelProviders.of(this).get(CourseViewModel.class);
// courseViewModel.getCoursesById().observe(this, new Observer<List<TermEntity>>() {
// @Override
// public void onChanged(List<TermEntity> termEntities) {
// adapter.setTerms(termEntities);
// }
// });
CourseDao:
@Dao
public interface CourseDao {
/**
* Insertion queries
* insertCourse inserts one course
* insertAll inserts all courses entered - not implemented
*/
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertCourse(CourseEntity course);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertAll(List<CourseEntity> courses);
/**
* Update queries
* updateCourse updates the currently selected course
*/
@Update(onConflict = OnConflictStrategy.REPLACE)
void updateCourse(CourseEntity course);
/**
* Deletion queries
* deleteCourse deletes the currently selected course
* deleteAll deletes all courses in the table
*/
@Delete
void deleteCourse(CourseEntity course);
@Query("DELETE FROM courses")
void deleteAllCourses();
/**
* Return queries
* getAll returns all courses ordered by name
* getCourseById returns a course that matches the selected course id
* getCoursesByTerm returns all courses that match the selected term id
*/
@Query("SELECT * FROM courses ORDER BY courseStartDate DESC")
LiveData<List<CourseEntity>> getAllCourses();
@Query("SELECT * FROM courses WHERE courseId = :courseId")
LiveData<List<CourseEntity>> getCourseById(int courseId);
@Query("SELECT * FROM courses WHERE termId = :termId")
LiveData<List<CourseEntity>> getCoursesByTerm(final int termId);
}
应用程序存储库课程代码:
/*
* Course Queries
*/
public void insertCourse(CourseEntity course) {
new InsertCourseAsyncTask(courseDao).execute(course);
}
private static class InsertCourseAsyncTask extends AsyncTask<CourseEntity, Void, Void> {
private CourseDao courseDao;
private InsertCourseAsyncTask(CourseDao courseDao) {
this.courseDao = courseDao;
}
@Override
protected Void doInBackground(CourseEntity... courseEntities) {
courseDao.insertCourse(courseEntities[0]);
return null;
}
}
public void updateCourse(CourseEntity course) {
new UpdateCourseAsyncTask(courseDao).execute(course);
}
private static class UpdateCourseAsyncTask extends AsyncTask<CourseEntity, Void, Void> {
private CourseDao courseDao;
private UpdateCourseAsyncTask(CourseDao courseDao) {
this.courseDao = courseDao;
}
@Override
protected Void doInBackground(CourseEntity... courseEntities) {
courseDao.updateCourse(courseEntities[0]);
return null;
}
}
public void deleteCourse(CourseEntity course) {
new DeleteCourseAsyncTask(courseDao).execute(course);
}
private static class DeleteCourseAsyncTask extends AsyncTask<CourseEntity, Void, Void> {
private CourseDao courseDao;
private DeleteCourseAsyncTask(CourseDao courseDao) {
this.courseDao = courseDao;
}
@Override
protected Void doInBackground(CourseEntity... courseEntities) {
courseDao.deleteCourse(courseEntities[0]);
return null;
}
}
public void deleteAllCourses() {
new DeleteAllCoursesAsyncTask(courseDao).execute();
}
private static class DeleteAllCoursesAsyncTask extends AsyncTask<Void, Void, Void> {
private CourseDao courseDao;
private DeleteAllCoursesAsyncTask(CourseDao courseDao) {
this.courseDao = courseDao;
}
@Override
protected Void doInBackground(Void... voids) {
courseDao.deleteAllCourses();
return null;
}
}
public LiveData<List<CourseEntity>> getAllCourses() {
return allCourses;
}
public LiveData<List<CourseEntity>> getAllAssignedCourses(final int termId) {
return courseDao.getCoursesByTerm(termId);
}
CourseViewModel:
public class CourseViewModel extends AndroidViewModel {
private AppRepository repository;
private LiveData<List<CourseEntity>> allCourses;
private LiveData<List<CourseEntity>> allAssignedCourses;
public CourseViewModel(@NonNull Application application) {
super(application);
repository = new AppRepository(application);
allCourses = repository.getAllCourses();
//allAssignedCourses = repository.getAllAssignedCourses(termId);
}
public void insertCourse(CourseEntity course) {
repository.insertCourse(course);
}
public void updateCourse(CourseEntity course) {
repository.updateCourse(course);
}
public void deleteCourse(CourseEntity course) {
repository.deleteCourse(course);
}
public void deleteAllCourses() {
repository.deleteAllCourses();
}
public LiveData<List<CourseEntity>> getAllCourses() {
return allCourses;
}
// public LiveData<List<CourseEntity>> getAllAssignedCourses(final int termId) {
// return allAssignedCourses;
// }
}