'How to pass CheckBox state from activity to a fragment
My application background:
This is a workout monitoring app.
What I want to do:
If the user clicks on the CheckBox, exercise progress has to be updated, for example
And I want to update adequate TextView (in this case for squats) to the current level.
What my app currently does:
I have a RecyclerView adapter for exercises activity. I've managed to save the checked state of the CheckBox and retrieve it. TinyDB is just a library I guess similar to SharedPreferences.
Class beginning:
public class ExercisesRecyclerView extends RecyclerView.Adapter<ExercisesRecyclerView.MyViewHolder>
{
String[] data1, data2, videoURL;
String exercise;
int[] images;
Context context;
TinyDBManager tinyDB;
public ExercisesRecyclerView(Context ct, String[] s1, String[] s2, String[] videoArray,
String exerciseType) {
context = ct;
data1 = s1;
data2 = s2;
videoURL = videoArray;
exercise = exerciseType;
tinyDB = TinyDB.getInstance(context);
}
The code to save CheckBox state:
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if(isChecked){
tinyDB.putBoolean("checkBox"+data2[position]+exercise, true);
}else{
tinyDB.putBoolean("checkBox"+data2[position]+exercise, false);
}
}
});
TinyDBManager manager2 = TinyDB.getInstance(context);
if(manager2.getBoolean("checkBox" + data2[position] + exercise, false)) {
holder.checkBox.setChecked(true);
}else{
holder.checkBox.setChecked(false);
}
Exercises Activity:
public class exercises extends AppCompatActivity {
String data1;
RecyclerView recyclerView;
String[] s1,s2,s3;
int[] images = {R.drawable.squat,R.drawable.pull_up,R.drawable.handstand,R.drawable.leg_raises,
R.drawable.push_up,R.drawable.dips,R.drawable.horizontal_pull,R.drawable.plank};
ImageView goBackArrow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exercises);
Objects.requireNonNull(getSupportActionBar()).hide();
goBackArrow = findViewById(R.id.go_back_arrow2);
data1 = getIntent().getStringExtra("data1");
Log.d("IntentToExercises", "onCreate: " + data1);
recyclerView = findViewById(R.id.exercisesRecyclerView);
if(data1.equals("SQUATS")) {
s1 = getResources().getStringArray(R.array.LEVEL);
s2 = getResources().getStringArray(R.array.SQUATS);
s3 = getResources().getStringArray(R.array.SQUATS_VIDEOURL);
ExercisesRecyclerView exercisesRecyclerView = new ExercisesRecyclerView(this,
s1,s2,s3,"squat");
recyclerView.setAdapter(exercisesRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
} else if(data1.equals("PULL UPS")){
s1 = getResources().getStringArray(R.array.LEVEL);
s2 = getResources().getStringArray(R.array.PULL_UPS);
s3 = getResources().getStringArray(R.array.PULL_UPS_VIDEOURL);
ExercisesRecyclerView exercisesRecyclerView = new ExercisesRecyclerView(this,
s1,s2,s3,"pullup");
recyclerView.setAdapter(exercisesRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}else if(data1.equals("HANDSTAND PUSH UPS")){
s1 = getResources().getStringArray(R.array.LEVEL);
s2 = getResources().getStringArray(R.array.HANDSTAND);
s3 = getResources().getStringArray(R.array.HANDSTAND_VIDEOURL);
ExercisesRecyclerView exercisesRecyclerView = new ExercisesRecyclerView(this,
s1,s2,s3,"handstand");
recyclerView.setAdapter(exercisesRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}else if(data1.equals("LEG RAISES")){
s1 = getResources().getStringArray(R.array.LEVEL);
s2 = getResources().getStringArray(R.array.LEGRAISES);
s3 = getResources().getStringArray(R.array.LEGRAISES_VIDEOURL);
ExercisesRecyclerView exercisesRecyclerView = new ExercisesRecyclerView(this,
s1,s2,s3,"legraises");
recyclerView.setAdapter(exercisesRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}else if(data1.equals("PUSH UPS")){
s1 = getResources().getStringArray(R.array.LEVEL);
s2 = getResources().getStringArray(R.array.PUSH_UPS);
s3 = getResources().getStringArray(R.array.PUSH_UPS_VIDEOURL);
ExercisesRecyclerView exercisesRecyclerView = new ExercisesRecyclerView(this,
s1,s2,s3,"pushups");
recyclerView.setAdapter(exercisesRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}else if(data1.equals("DIPS")){
s1 = getResources().getStringArray(R.array.LEVEL);
s2 = getResources().getStringArray(R.array.DIPS);
s3 = getResources().getStringArray(R.array.DIPS_VIDEOURL);
ExercisesRecyclerView exercisesRecyclerView = new ExercisesRecyclerView(this,
s1,s2,s3,"dips");
recyclerView.setAdapter(exercisesRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}else if(data1.equals("HORIZONTAL PULLS")){
s1 = getResources().getStringArray(R.array.LEVEL);
s2 = getResources().getStringArray(R.array.HORIZONTAL_PULLS);
s3 = getResources().getStringArray(R.array.HORIZONTAL_PULLS_VIDEOURL);
ExercisesRecyclerView exercisesRecyclerView = new ExercisesRecyclerView(this,
s1,s2,s3,"horizontalpulls");
recyclerView.setAdapter(exercisesRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
My guess is that I have to assign indexes for each CheckBox, add all the data to an array or array list and then pass it to the exercises progress fragment and then loop through this array to find the highest index which would be the level that the user is currently on, but I don't know if I am guessing correctly. If my guess is correct, then I can somehow work with that, but I need some guidance.
Solution 1:[1]
First of all, i would recommend code refactorization to your exercises activity, because you've got a lot of dupicated code, and also, it isn't a good practice to name your activity in lower case.
At last, from your fragment, you've got access to the activity containing your instance. If you cast in your fragment yourFragment.getActivity() to "exercises", and implement in you class a method which implements your desired logic, you're problem will be solved.
I wish i helped, please let me know if i can help in anything else. :)
Refactorization: If you create a class with s1, s2 and s3, then inside you if cases you would just instantiate that object. In this way your code will be much clear simple.
YourObject object = null
if (firstCase) {object = YourObject(LEVEL, "pushups", "ksajdfh");}
else if (secondCase) {object = YourObject(LEVEL, "run", "ksajdfh");}
......
ExercisesRecyclerView exercisesRecyclerView = new ExercisesRecyclerView(this,
object.s1,object.s2,object.s3,object.s4);
recyclerView.setAdapter(exercisesRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


