如何将复选框状态从活动传递到片段
我的应用程序背景:
这是一个健身监视应用程序。
我想做的:
如果用户单击复选框,则必须更新练习进度,例如
,我想更新足够的textview(在这种情况下为squats for squats)到当前级别。
我的应用程序当前要做的事情:
我有一个用于练习活动的回收天口适配器。我设法保存了复选框的检查状态并检索它。我猜TinyDB只是一个类似于共享流程的库。
课程开始:
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);
}
保存复选框状态的代码:
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);
}
练习活动:
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我会建议您的练习活动进行代码重构,因为您有很多dupicated代码,而且在较低情况下命名您的活动并不是一个好习惯。
最后,从片段中,您可以访问包含实例的活动。如果您将fragment your fragment.getactivity()放在“练习”中,并在您的类中实现一种实现您所需逻辑的方法,那么您的问题将得到解决。
我希望我有帮助,请让我知道我是否可以帮助其他任何事情。 :)
重构:
如果您使用S1,S2和S3创建类,则在您的内部使用,如果您只需实例化该对象。这样,您的代码将非常简单。
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.