2

I'm using the Groupie library for showing expandable items and my issue is that instead of the items retrieved through an api call being shown under each of their parent expandable header, they're all shown together under the last item on the list.

I would like to have this:

  • Header 1
    • Child header 1
    • Child header 1
    • Child header 1
  • Header 2
    • Child header 2
    • Child header 2

I have this instead:

  • Header 1
  • header 2
    • Child header 1
    • Child header 1
    • Child header 1
    • Child header 2
    • Child header 2

This is my code

 private fun updateUICurrentExercise(getCurrentWorkoutExercise: GetCurrentWorkoutExercise?) {

    adapter = GroupAdapter()
    val workouts = getCurrentWorkoutExercise?.workouts

    for (w in workouts!!) {

        exp = ExpandableGroup(WorkoutItem("Workout ${(w.workoutId)}"), false)
        getExercisesByWorkoutAPI(w.workoutId.toString())

        adapter.add(exp)
    }

    rvWorkout.adapter = adapter
}

 private fun getExercisesByWorkoutAPI(workoutId: String) {

    GetExercisesByWorkoutAPI.postData(jo, object : GetExercisesByWorkoutAPI.ThisCallback {
        override fun onSuccess(getExercisesByWorkout: GetExercisesByWorkout?) {
           
            for (e in getExercisesByWorkout?.exercises!!) {

                exp.add(Section(ExerciseItem(workoutId)))
            }
        }

    })
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81

2 Answers2

2

It looks like you missed to use Section.setHeader. So you will need to create one header instance WorkoutItem("Workout ${(w.workoutId)}" for each w collection item and then pass this instance into ExpandableGroup constructor and Section.setHeader

Valerii
  • 395
  • 1
  • 2
  • 8
  • Hi @valerii, thank you for your reply. I hear what you're saying and understand it, just not sure if able to apply it as it's my first week trying the library so am still getting my head around it. But will try your suggestion now and if able to make it work will make sure I come back here and have it as the accepted answer. Thanks again. :) – Francislainy Campos Feb 03 '19 at 19:42
  • Hi @valerii, I think the problem may be actually coming from waiting for the api response. I played a bit with the section headers but the outcome seems to be the same as when I paste this line `exp.add(Section(ExerciseItem(workoutId)))` before the onSuccess from the api, I get the items to appear under their parents but when I put them inside the api call they all appear just under the last item. The issue is I'm now even more stuck because I probably need something like observers to have my items populate just when the api call is finished or something similar and i really don't know them. – Francislainy Campos Feb 03 '19 at 22:01
  • Then this code will probably work for you. `override fun onSuccess(getExercisesByWorkout: GetExercisesByWorkout?) {` `val expandableGroup = ExpandableGroup(WorkoutItem("Workout $workoutId"), false)` `for (e in getExercisesByWorkout?.exercises!!) {` `expandableGroup.add(Section(ExerciseItem(workoutId)))` `}` `adapter.add(expandableGroup)` `}` – Valerii Feb 04 '19 at 07:57
  • I recommend you to take a look at rxjava+retrofit for rest api calls. RxJava is based on the idea of Observer pattern and provides the opportunity to conveniently switch between threads. I used rxjava2 and retrofit2 in my following projects. https://github.com/ValeriiVolkov/Weather https://github.com/ValeriiVolkov/Recipes – Valerii Feb 04 '19 at 07:58
  • Thanks very much @Valerii. It worked now and I've marked your answer as accepted. It makes totally sense to have both the expandable group and its items initiated on the same api call. I've as one of my plans to learn RXJava as I keep running into problems of these kind when one api finishes first than the other. Thanks again for your help. Much appreciated. :) – Francislainy Campos Feb 04 '19 at 18:42
  • @FrancislainyCampos I'm happy to help you) You're welcome :) – Valerii Feb 04 '19 at 18:46
2

Based on @valerii's reply that's the fix for the issue: having both the expandable group and its items initiated within the same api call.

private fun updateUICurrentExercise(getCurrentWorkoutExercise: GetCurrentWorkoutExercise?) {

    adapter = GroupAdapter()
    val workouts = getCurrentWorkoutExercise?.workouts

    for (w in workouts!!) {

        getExercisesByWorkoutAPI(w.workoutId.toString())
    }

    rvWorkout.adapter = adapter
}

 private fun getExercisesByWorkoutAPI(workoutId: String) {

    GetExercisesByWorkoutAPI.postData(jo, object : GetExercisesByWorkoutAPI.ThisCallback {
        override fun onSuccess(getExercisesByWorkout: GetExercisesByWorkout?) {

            val expandableGroup = ExpandableGroup(WorkoutItem("Workout $workoutId"), false)
            for (e in getExercisesByWorkout?.exercises!!) {
                expandableGroup.add(Section(ExerciseItem(e.exerciseName)))
            }
            adapter.add(expandableGroup)
        }
    })
}
Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81