LBJ33 LBJ33 - 3 months ago 54
Android Question

RecyclerView Checkboxes lose value when layout switched

I'm having trouble with my recyclerview. It seems whenever the phone is turned into landscape mode, or the layout is switched in the app, the previously selected checkboxes are not restored.

EDIT------------------------------------------------

Here is my recycler adapter:

public MyRecyclerAdapter(Context context, ArrayList<Workout> workout) {
mContext = context;
this.workout = workout;
}
// INITIALIZE HOLDER
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.workout_item, null);
MyViewHolder holder = new MyViewHolder(view);

return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.exercise.setText(workout.get(position).getExercise());
holder.percent.setText(workout.get(position).getPercent());
holder.reps.setText(workout.get(position).getReps());
holder.weight.setText(workout.get(position).getWeight());
holder.check1.setOnCheckedChangeListener(null);
final Workout isCheck = workout.get(position);
holder.check1.setChecked(isCheck.isCheck1());


holder.check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isCheck.setCheck1(isChecked);
}
});

}


This is where the adapter is set in each fragment class(this is in the onCreateView):

//RECYCLERVIEW
RecyclerView rv = (RecyclerView) view.findViewById(R.id.mRecyclerWed);
rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
rv.setAdapter(new MyRecyclerAdapter(this.getActivity(), getWedWorkout()));
return view;
}


And here is where the fragments are set up:

public class WorkoutDaysActivity extends BaseActivity{

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_days);
mToolBar = activateToolbar();
setUpNavigationDrawer();
getSupportActionBar().setTitle("Workout");

ViewPager vp = (ViewPager) findViewById(R.id.view_pager);
this.addPages(vp);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(vp);
tabLayout.setOnTabSelectedListener(listener(vp));

}

//ADD ALL PAGES
private void addPages(ViewPager pager) {
MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
Intent intent = getIntent();
String workout = intent.getStringExtra("workout");
Bundle args = new Bundle();
args.putString("workout", workout);

MondayFragment mondayFragment = new MondayFragment();
mondayFragment.setArguments(args);
adapter.addPage(mondayFragment);
WedFragment wedFragment = new WedFragment();
wedFragment.setArguments(args);
adapter.addPage(wedFragment);
FridayFragment fridayFragment = new FridayFragment();
fridayFragment.setArguments(args);
adapter.addPage(fridayFragment);
pager.setAdapter(adapter);
}

Answer

That's right, the fragments get re-created everytime you close it and reopen it, the same happens when the orientation changes. To answer your question, yes! There are some ways to solve that, I would use SharedPraferences to save the state of the checkbox and restore it back to that state. Example?

    int firstSecondOrThird; //first page, second page or thrid page.

    public MyRecyclerAdapter(Context context, ArrayList<Workout> workout, int thePosition) {
        mContext = context;
        this.workout = workout;
        this.firstSecondOrThird = thePosition;
    }


    final SharedPreferences prefs;

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.exercise.setText(workout.get(position).getExercise());
        //...
        prefs = mContext.getSharedPreferences("ehehehe", Context.MODE_PRIVATE);

        holder.check1.setChecked(prefs.getBoolean(firstSecondOrThird+"checkState"+position, false)); //restore the state of the CheckBox
        isCheck.setCheck1(prefs.getBoolean(firstSecondOrThird+"checkState"+position, false)); //not sure what's this just copying from you ^^


        holder.check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isCheck.setCheck1(isChecked);
                prefs.edit().putBoolean(firstSecondOrThird+"checkState"+position, isChecked).apply(); //save the CheckBox's state
            }
        });

    }

And initiate it like this:

    RecyclerView rv = (RecyclerView) view.findViewById(R.id.mRecyclerWed);
    rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    rv.setAdapter(new MyRecyclerAdapter(this.getActivity(), getWedWorkout(), 1)); //in your first page

    RecyclerView rv = (RecyclerView) view.findViewById(R.id.mRecyclerWed);
    rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    rv.setAdapter(new MyRecyclerAdapter(this.getActivity(), getWedWorkout(), 2)); //in your second page, and 3 or the third
Comments