ActiveAndroid Many-to-many relationship

前端 未结 3 1498
忘掉有多难
忘掉有多难 2021-01-05 06:33

I’m currently using ActiveAndroid, and have been trying to get a many-to-many relationship to work for the past few hours, however I just can’t get it to work. I hope you ca

3条回答
  •  执笔经年
    2021-01-05 06:55

    If I change the cast of (Student) to (StudentCourse) I get the following error...

    The cast should actually be to (List), but the real problem is in the logic of your model here. You are calling executeSingle(), but you really want multiple StudentCourse objects so that you get every Student-Course relationship for the Course. Your students() and courses() methods don't make much sense, since one StudentCourse object only has one Student and one Course.

    I would do it like so instead:

    List enrolments = new Select().from(StudentCourse.class).where("course = ?",selectedCourse.getId()).execute();
    
    List studentsInCourse = new ArrayList();
    for(StudentCourse enrolment:enrolments)
        studentsInCourse.add(enrolment.student);
    

提交回复
热议问题