I'm working on an App to manage my workouts based on a room database.
Here I have a recyclerview with adapter for exercises in a workout. My current problem is that new exercises are not shown in the recyclerview after creating them and switching back to the workout-activity.
It seems like a quite easy topic (update recyclerview after returning from other activity) but I cant' get it working.
initial load of exercises at onCreate of workout-Activity:
class GetExercises extends AsyncTask<Long, Void, List<Exercise>>{ @Override protected List<Exercise> doInBackground(Long... params){ if (params.length == 0 || params.length > 1) return null; return DatabaseClient .getInstance(getApplicationContext()) .getAppDatabase().exerciseDao() .getReferenced(params[0]); } protected void onPostExecute(List<Exercise> exerciseList){ super.onPostExecute(exerciseList); rv_exercises.setAdapter(new Adapter_Exercise_RV(Activity_Workout_Edit.this, exerciseList)); } }creating a new exercise and returning the id to previous workout-activity:
class CreateExercise extends AsyncTask<Void, Void, Void> { long id; @Override protected Void doInBackground(Void... voids) { Exercise ex = new Exercise(); ex.setTitle(sTitle); ex.setSet(sSet); ex.setRep(sRep); ex.setAsTimer(bAsTimer); ex.setWorkoutId(workoutId); id = DatabaseClient.getInstance(getApplicationContext()) .getAppDatabase() .exerciseDao() .insertwithRet(ex); return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); finish(id); Toast.makeText(getApplicationContext(), "created new exercise", Toast.LENGTH_LONG).show(); } } new CreateExercise().execute();update adapter onActivityResult at workout-activity
protected void onActivityResult(int requestCode, int resultCode, Intent data){ super.onActivityResult(requestCode, resultCode, data); if(requestCode == 1 && resultCode == RESULT_OK){ long exerciseId = (long) data.getExtras().get("exerciseId"); rv_exercises.getAdapter().notifyDataSetChanged(); } }
the exercise is created in the database, and after reopening the workout the new exercise is shown correctly. I do have a feeling that "notifyDataSetChanged" is not working, but can't find why?