我试图将复选框和数据从android studio中的多个活动传递到一个活动,即使用RuleBasedActivity.javaSharedPreferences

然后在RuleBasedActivity中,我使用if else语句在另一个活动中显示结果,该活动基于用户的复选框打勾而在HeartDiseaseResult.java中。

RuleBasedActivity中,我尝试编写一个简单的if else条件测试,以成功显示结果。结果应该是心脏病的低风险可能性,高风险,可能性很小或其他。

例如,如果用户勾选复选框leftArm || bothArms,则应显示“心脏病风险高”为真,如果用户勾选复选框NoneOther || radioMale || sweating || jaw,则应显示“心脏病风险低”为真,但不幸的是,它显示所有4个结果。

请,希望在这里得到一些帮助。

TwoDActitivty1.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two_d1);

        buttonHome = (Button) findViewById(R.id.buttonHome);
        buttonBackBody = (Button) findViewById(R.id.buttonBackBody);
        buttonNext = (Button) findViewById(R.id.buttonNext);

        head = (Button) findViewById(R.id.head);
        neck = (Button) findViewById(R.id.neck);
        shoulder = (Button) findViewById(R.id.shoulder);
        chest = (Button) findViewById(R.id.chest);
        abdominal = (Button) findViewById(R.id.abdominal);
        arm = (Button) findViewById(R.id.arm);
        pulse = (Button) findViewById(R.id.pulse);

        buttonHome.setOnClickListener(this);
        buttonBackBody.setOnClickListener(this);
        buttonNext.setOnClickListener(this);


        head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                showUpdateDialog();
            }
        });

        neck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                showUpdateDialog1();
            }
        });

        shoulder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                showUpdateDialog2();
            }
        });

        chest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                showUpdateDialog3();
            }
        });


        arm.setOnClickListener(
                new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                showUpdateDialog6();
            }
        });
    }

    private void showUpdateDialog() {

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

        LayoutInflater inflater = getLayoutInflater();

        final View dialogView = inflater.inflate(R.layout.checkbox_head, null);

        dialogBuilder.setView(dialogView);

        dialogBuilder.setTitle("Did the pain / discomfort spread to:");

        final CheckBox dizziness = (CheckBox) dialogView.findViewById(R.id.dizziness);
        final CheckBox lightheadedness = (CheckBox) dialogView.findViewById(R.id.lightheadedness);
        final CheckBox fatigue = (CheckBox) dialogView.findViewById(R.id.fatigue);
        final CheckBox sleepDisturbance = (CheckBox) dialogView.findViewById(R.id.sleepDisturbance);
        final CheckBox stress = (CheckBox) dialogView.findViewById(R.id.stress);
        final CheckBox nausea = (CheckBox) dialogView.findViewById(R.id.nausea);
        final CheckBox vomiting = (CheckBox) dialogView.findViewById(R.id.vomiting);
        final CheckBox shortnessOfBreath = (CheckBox) dialogView.findViewById(R.id.shortnessOfBreath);
        final CheckBox NoneHead = (CheckBox) dialogView.findViewById(R.id.NoneHead);
        final Button save = (Button) dialogView.findViewById(R.id.save);
        final Button cancel = (Button) dialogView.findViewById(R.id.cancel);

        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("symptom_list", MODE_PRIVATE);
        final SharedPreferences.Editor editor = sharedPreferences.edit();

        final AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (dizziness.isChecked()) {
                    editor.putBoolean("dizziness", true);
                }

                if (lightheadedness.isChecked()) {
                    editor.putBoolean("lightheadedness", true);
                }

                if (fatigue.isChecked()) {
                    editor.putBoolean("fatigue", true);
                }

                if (sleepDisturbance.isChecked()) {
                    editor.putBoolean("sleepDisturbance", true);
                }

                if (stress.isChecked()) {
                    editor.putBoolean("stress", true);
                }

                if (nausea.isChecked()) {
                    editor.putBoolean("nausea", true);
                }

                if (vomiting.isChecked()) {
                    editor.putBoolean("vomiting", true);
                }

                if (shortnessOfBreath.isChecked()) {
                    editor.putBoolean("shortnessOfBreath", true);
                }

                if (NoneHead.isChecked()) {
                    editor.putBoolean("NoneHead", true);
                }
                editor.apply();
                Toast.makeText(getApplicationContext(), "SAVED", Toast.LENGTH_SHORT).show();
                alertDialog.dismiss();
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });
    }

    private void showUpdateDialog1() {

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

        LayoutInflater inflater = getLayoutInflater();

        final View dialogView = inflater.inflate(R.layout.checkbox_neck, null);

        dialogBuilder.setView(dialogView);

        dialogBuilder.setTitle("Please select the best one");
        dialogBuilder.setMessage("Where does the pain / discomfort spread:");

        final CheckBox throatOrNeck = (CheckBox) dialogView.findViewById(R.id.throatOrNeck);
        final CheckBox jaw = (CheckBox) dialogView.findViewById(R.id.jaw);
        final CheckBox backOfHeadAndNeck = (CheckBox) dialogView.findViewById(R.id.backOfHeadAndNeck);
        final CheckBox NoneNeck = (CheckBox) dialogView.findViewById(R.id.NoneNeck);
        final Button save = (Button) dialogView.findViewById(R.id.save);
        final Button cancel = (Button) dialogView.findViewById(R.id.cancel);

        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("symptom_list", MODE_PRIVATE);
        final SharedPreferences.Editor editor = sharedPreferences.edit();

        final AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (throatOrNeck.isChecked()) {
                    editor.putBoolean("throatOrNeck", true);
                }

                if (jaw.isChecked()) {
                    editor.putBoolean("jaw", true);
                }

                if (backOfHeadAndNeck.isChecked()) {
                    editor.putBoolean("backOfHeadAndNeck", true);
                }

                if (NoneNeck.isChecked()) {
                    editor.putBoolean("NoneNeck", true);
                }

                editor.apply();
                Toast.makeText(getApplicationContext(), "SAVED", Toast.LENGTH_SHORT).show();
                alertDialog.dismiss();
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });
    }

    private void showUpdateDialog2() {

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

        LayoutInflater inflater = getLayoutInflater();

        final View dialogView = inflater.inflate(R.layout.checkbox_shoulder, null);

        dialogBuilder.setView(dialogView);

        dialogBuilder.setTitle("Please select the best one");
        dialogBuilder.setMessage("Where does the pain / discomfort spread:");

        final CheckBox leftShoulder = (CheckBox) dialogView.findViewById(R.id.leftShoulder);
        final CheckBox bothShoulder = (CheckBox) dialogView.findViewById(R.id.bothShoulder);
        final CheckBox NoneShoulder = (CheckBox) dialogView.findViewById(R.id.NoneShoulder);
        final Button save = (Button) dialogView.findViewById(R.id.save);
        final Button cancel = (Button) dialogView.findViewById(R.id.cancel);

        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("symptom_list", MODE_PRIVATE);
        final SharedPreferences.Editor editor = sharedPreferences.edit();

        final AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (leftShoulder.isChecked()) {
                    editor.putBoolean("leftShoulder", true);
                }

                if (bothShoulder.isChecked()) {
                    editor.putBoolean("bothShoulder", true);
                }

                if (NoneShoulder.isChecked()) {
                    editor.putBoolean("NoneShoulder", true);
                }

                editor.apply();
                Toast.makeText(getApplicationContext(), "SAVED", Toast.LENGTH_SHORT).show();
                alertDialog.dismiss();

            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });

    }

    private void showUpdateDialog3() {

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

        LayoutInflater inflater = getLayoutInflater();

        final View dialogView = inflater.inflate(R.layout.checkbox_chest, null);

        dialogBuilder.setView(dialogView);

        dialogBuilder.setTitle("Please select the best one");
        dialogBuilder.setMessage("Which of these best describes Chest pain / discomfort that you feel:");

        final CheckBox chest1 = (CheckBox) dialogView.findViewById(R.id.chest1);
        final CheckBox chest2 = (CheckBox) dialogView.findViewById(R.id.chest2);
        final CheckBox chest3 = (CheckBox) dialogView.findViewById(R.id.chest3);
        final CheckBox chest4 = (CheckBox) dialogView.findViewById(R.id.chest4);
        final CheckBox chest5 = (CheckBox) dialogView.findViewById(R.id.chest5);
        final CheckBox chest6 = (CheckBox) dialogView.findViewById(R.id.chest6);
        final CheckBox chest7 = (CheckBox) dialogView.findViewById(R.id.chest7);
        final CheckBox NoneChest = (CheckBox) dialogView.findViewById(R.id.NoneChest);
        final Button save = (Button) dialogView.findViewById(R.id.save);
        final Button cancel = (Button) dialogView.findViewById(R.id.cancel);

        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("symptom_list", MODE_PRIVATE);
        final SharedPreferences.Editor editor = sharedPreferences.edit();

        final AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (chest1.isChecked()) {
                    editor.putBoolean("chest1", true);
                }

                if (chest2.isChecked()) {
                    editor.putBoolean("chest2", true);
                }

                if (chest3.isChecked()) {
                    editor.putBoolean("chest3", true);
                }

                if (chest4.isChecked()) {
                    editor.putBoolean("chest4", true);
                }

                if (chest5.isChecked()) {
                    editor.putBoolean("chest5", true);
                }

                if (chest6.isChecked()) {
                    editor.putBoolean("chest6", true);
                }

                if (chest7.isChecked()) {
                    editor.putBoolean("chest7", true);
                }

                if (NoneChest.isChecked()) {
                    editor.putBoolean("NoneChest", true);
                }

                editor.apply();
                Toast.makeText(getApplicationContext(), "SAVED", Toast.LENGTH_SHORT).show();
                alertDialog.dismiss();

            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });
    }
        private void showUpdateDialog6() {

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

        LayoutInflater inflater = getLayoutInflater();

        final View dialogView = inflater.inflate(R.layout.checkbox_arm, null);

        dialogBuilder.setView(dialogView);

        dialogBuilder.setTitle("Please select the best one");
        dialogBuilder.setMessage("Where does the pain / discomfort spread:");

        final CheckBox leftArm = (CheckBox) dialogView.findViewById(R.id.leftArm);
        final CheckBox bothArms = (CheckBox) dialogView.findViewById(R.id.bothArms);
        final CheckBox NoneArm = (CheckBox) dialogView.findViewById(R.id.NoneArm);
        final Button save = (Button) dialogView.findViewById(R.id.save);
        final Button cancel = (Button) dialogView.findViewById(R.id.cancel);

        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("symptom_list", MODE_PRIVATE);
        final SharedPreferences.Editor editor = sharedPreferences.edit();

        final AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (leftArm.isChecked()) {
                    editor.putBoolean("leftArm", true);
                }

                if (bothArms.isChecked()) {
                    editor.putBoolean("bothArms", true);
                }

                if (NoneArm.isChecked()) {
                    editor.putBoolean("NoneArm", true);
                }

                editor.apply();
                Toast.makeText(getApplicationContext(), "SAVED", Toast.LENGTH_SHORT).show();
                alertDialog.dismiss();
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });

    }
}


TwoDActivity2.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two_d2);


        buttonHome = (Button) findViewById(R.id.buttonHome);
        buttonFrontBody = (Button) findViewById(R.id.buttonFrontBody);
        backNeck = (Button) findViewById(R.id.backNeck);
        skin = (Button) findViewById(R.id.skin);

        buttonHome.setOnClickListener(this);
        buttonFrontBody.setOnClickListener(this);

        backNeck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                showUpdateDialog();
            }
        });

        skin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                showUpdateDialog1();
            }
        });

    }

    private void showUpdateDialog() {

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

        LayoutInflater inflater = getLayoutInflater();

        final View dialogView = inflater.inflate(R.layout.checkbox_backneck, null);

        dialogBuilder.setView(dialogView);

        dialogBuilder.setTitle("Does the pain / discomfort spread:");

        final CheckBox backNeck = (CheckBox) dialogView.findViewById(R.id.backNeck);
        final Button save = (Button) dialogView.findViewById(R.id.save);
        final Button cancel = (Button) dialogView.findViewById(R.id.cancel);

        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("symptom_list", MODE_PRIVATE);
        final SharedPreferences.Editor editor = sharedPreferences.edit();

        final AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (backNeck.isChecked()) {
                    editor.putBoolean("backNeck", true);
                }
                editor.apply();
                Toast.makeText(getApplicationContext(), "SAVED", Toast.LENGTH_SHORT).show();
                alertDialog.dismiss();
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });
    }

    private void showUpdateDialog1() {

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

        LayoutInflater inflater = getLayoutInflater();

        final View dialogView = inflater.inflate(R.layout.checkbox_skin, null);

        dialogBuilder.setView(dialogView);

        dialogBuilder.setTitle("Other sign?");

        final CheckBox sweating = (CheckBox) dialogView.findViewById(R.id.sweating);
        final Button save = (Button) dialogView.findViewById(R.id.save);
        final Button cancel = (Button) dialogView.findViewById(R.id.cancel);

        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("symptom_list", MODE_PRIVATE);
        final SharedPreferences.Editor editor = sharedPreferences.edit();

        final AlertDialog alertDialog = dialogBuilder.create();
        alertDialog.show();

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (sweating.isChecked()) {
                    editor.putBoolean("sweating", true);
                }
                editor.apply();
                Toast.makeText(getApplicationContext(), "SAVED", Toast.LENGTH_SHORT).show();
                alertDialog.dismiss();
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });
    }
}


ReleBasedActivity.java

    boolean highRiskOfHeartDisease, lowRiskOfHeartDisease, unlikelyNoHeartDisease, other;
    private Button diagnose;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rule_based);

        final SharedPreferences sharedPreferences = getSharedPreferences("symptom_list", Context.MODE_PRIVATE);

        //from other activity(first page)
        final Boolean radioMale = sharedPreferences.getBoolean("radioMale", false);
        final Boolean radioFemaleNotMonopause = sharedPreferences.getBoolean("radioFemaleNotMonopause", false);
        final Boolean radioFemaleMonopause = sharedPreferences.getBoolean("radioFemaleMonopause", false);
        final Boolean BP = sharedPreferences.getBoolean("BP", false);
        final Boolean notActive = sharedPreferences.getBoolean("notActive", false);
        final Boolean diabetic = sharedPreferences.getBoolean("diabetic", false);
        final Boolean smoke = sharedPreferences.getBoolean("smoke", false);
        final Boolean familyHistory = sharedPreferences.getBoolean("familyHistory", false);
        final Boolean hadHeartAttackBefore = sharedPreferences.getBoolean("hadHeartAttackBefore", false);
        final Boolean NoneOther = sharedPreferences.getBoolean("NoneOther", false);


        //TwoDActivity1
        //checkbox from head button
        final Boolean dizziness = sharedPreferences.getBoolean("dizziness", false);
        final Boolean lightheadedness = sharedPreferences.getBoolean("lightheadedness", false);
        final Boolean fatigue = sharedPreferences.getBoolean("fatigue", false);
        final Boolean sleepDisturbance = sharedPreferences.getBoolean("sleepDisturbance", false);
        final Boolean stress = sharedPreferences.getBoolean("stress", false);
        final Boolean nausea = sharedPreferences.getBoolean("nausea", false);
        final Boolean vomiting = sharedPreferences.getBoolean("vomiting", false);
        final Boolean shortnessOfBreath = sharedPreferences.getBoolean("shortnessOfBreath", false);
        final Boolean NoneHead = sharedPreferences.getBoolean("NoneHead", false);

        //checkbox from neck button
        final Boolean throatOrNeck = sharedPreferences.getBoolean("throatOrNeck", false);
        final Boolean jaw = sharedPreferences.getBoolean("jaw", false);
        final Boolean backOfHeadAndNeck = sharedPreferences.getBoolean("backOfHeadAndNeck", false);
        final Boolean NoneNeck = sharedPreferences.getBoolean("NoneNeck", false);

        //checkbox from shoulder button
        final Boolean leftShoulder = sharedPreferences.getBoolean("leftShoulder", false);
        final Boolean bothShoulder = sharedPreferences.getBoolean("bothShoulder", false);
        final Boolean NoneShoulder = sharedPreferences.getBoolean("NoneShoulder", false);

        //checkbox from chest button
        final Boolean chest1 = sharedPreferences.getBoolean("chest1", false);
        final Boolean chest2 = sharedPreferences.getBoolean("chest2", false);
        final Boolean chest3 = sharedPreferences.getBoolean("chest3", false);
        final Boolean chest4 = sharedPreferences.getBoolean("chest4", false);
        final Boolean chest5 = sharedPreferences.getBoolean("chest5", false);
        final Boolean chest6 = sharedPreferences.getBoolean("chest6", false);
        final Boolean chest7 = sharedPreferences.getBoolean("chest7", false);
        final Boolean NoneChest = sharedPreferences.getBoolean("NoneChest", false);

        //checkbox from arm button
        final Boolean leftArm = sharedPreferences.getBoolean("leftArm", false);
        final Boolean bothArms = sharedPreferences.getBoolean("bothArms", false);
        final Boolean NoneArm = sharedPreferences.getBoolean("NoneArm", false);


        //TwoDActivity2
        //checkbox from backNeck
        final Boolean backNeck = sharedPreferences.getBoolean("backNeck", false);

        //checkbox from skin
        final Boolean sweating = sharedPreferences.getBoolean("sweating", false);


        diagnose = (Button) findViewById(R.id.diagnose);

        diagnose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                highRiskOfHeartDisease = true;
                lowRiskOfHeartDisease = true;
                unlikelyNoHeartDisease = true;
                other = true;

                if (leftArm || bothArms) {
                    highRiskOfHeartDisease = true;
                }
                else if (lightheadedness || leftShoulder) {
                    unlikelyNoHeartDisease = true;
                }
                else if (chest1) {
                    other = true;
                }
                else if (NoneOther || radioMale || sweating || jaw) {
                    lowRiskOfHeartDisease = true;
                }


                Intent intentBundle = new Intent(getBaseContext(), HeartSymptomActivity.class);
                Bundle bundle = new Bundle();
                bundle.putBoolean("highRisk", highRiskOfHeartDisease);
                bundle.putBoolean("lowRisk", lowRiskOfHeartDisease);
                bundle.putBoolean("unlikely", unlikelyNoHeartDisease);
                bundle.putBoolean("other", other);
                intentBundle.putExtras(bundle);
                startActivity(intentBundle);
            }
        });



    }
}


HeartSymptomResult.java

    boolean highRisk, lowRisk, unlikely, other;
    private Button clear;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_heart_symptom);

        addData();

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycleView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        TextAdapter textAdapter = new TextAdapter();
        recyclerView.setAdapter(textAdapter);

        List<String> stringList = new ArrayList<>();

        if (highRisk) {
            stringList.add("High Risk of Heart Disease" + highRisk);
        }

        if (lowRisk) {
            stringList.add("Low Risk of Heart Disease" + lowRisk);
        }

        if (unlikely) {
            stringList.add("Maybe no sign Heart Disease" + unlikely);
        }

        if (other) {
            stringList.add("Try test" + other);
        }

        List<String> list = new ArrayList<>();
        list.addAll(stringList);

        textAdapter.setmItems(list);
    }
    private void addData() {
        //intent Bundle
        Intent intentExtras = getIntent();
        Bundle extrasBundle = intentExtras.getExtras();

        if (extrasBundle != null) {
            highRisk = extrasBundle.getBoolean("highRisk");
            lowRisk = extrasBundle.getBoolean("lowRisk");
            unlikely = extrasBundle.getBoolean("unlikely");
            other = extrasBundle.getBoolean("other");
        }

    }


}

最佳答案

在任何情况下都不能将它们设置为false,这就是为什么所有if块都被执行的原因。默认情况下,将这些值初始化为false,然后根据情况设置为true。

highRiskOfHeartDisease = true;
lowRiskOfHeartDisease = true;
unlikelyNoHeartDisease = true;
other = true

08-17 02:56