samedi 20 octobre 2018

Pass Checkbox data from multiple dialogBox to another activity

I'm new with android studio and am not really an expert with programming, hope to get some help.

Now I'm developing a health application where user can click on the 2d image for their symptoms at the red button: 2D image with clickable button dialogBox with checkbox will appear when click on any red button on the image click on button in neck area and other dialog box will appear with list of checkbox. The problem is how do i pass all checkbox status from TwoDActivity.java(that contain the code of the multiple dialog box that contain list of checkbox in different dialog box) to RuleBasedActivity.java(if else condition statement from checkbox tick by user). i have already tried to use intent but it can only pass/saved checkbox data from ONE dialog box. Can i used sharedPreference to pass/saved the checkbox data to RuleBasedActivity? where should i code and how do i code sharedPreference to saved all checkbox data from multiple dialog box to another activity. Thank you in advanced.

TwoDActivity.java

    private Button buttonHome;
    private Button buttonBackBody;
    private Button buttonNext;
    private Button head;
    private Button neck;
    private Button shoulder;
    private Button chest;
    private Button abdominal;
    private Button arm;
    private Button pulse;


    @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();
            }
        });

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

                showUpdateDialog4();
            }
        });

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

                showUpdateDialog5();
            }
        });

        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.activity_checkbox_head, null);

        dialogBuilder.setView(dialogView);

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

    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);

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

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (throatOrNeck.isChecked()) {
                    Intent intent = new Intent(getApplicationContext(), RuleBasedActivity.class);
                    intent.putExtra("throatOrNeck", true);
                }

                else if (jaw.isChecked()) {
                    Intent intent = new Intent(getApplicationContext(), RuleBasedActivity.class);
                    intent.putExtra("jaw", true);
                }

                else if (backOfHeadAndNeck.isChecked()) {
                    Intent intent = new Intent(getApplicationContext(), RuleBasedActivity.class);
                    intent.putExtra("backOfHeadAndNeck", true);
                }

                else if (NoneNeck.isChecked()) {
                    Intent intent = new Intent(getApplicationContext(), RuleBasedActivity.class);
                    intent.putExtra("NoneNeck", true);
                }

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

            //Toast.makeText(getApplicationContext(), "SAVED", Toast.LENGTH_SHORT).show();
        });
        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.activity_checkbox_shoulder, null);

        dialogBuilder.setView(dialogView);

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

    private void showUpdateDialog3() {

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

        LayoutInflater inflater = getLayoutInflater();

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

        dialogBuilder.setView(dialogView);

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

    private void showUpdateDialog4() {

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

        LayoutInflater inflater = getLayoutInflater();

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

        dialogBuilder.setView(dialogView);


        dialogBuilder.setMessage("Do you feel heartburn:");

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

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

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (heartburn.isChecked()) {
                    Intent intent = new Intent(getApplicationContext(), RuleBasedActivity.class);
                    intent.putExtra("heartburn", true);
                }

                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 showUpdateDialog5() {

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

        LayoutInflater inflater = getLayoutInflater();

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

        dialogBuilder.setView(dialogView);

        dialogBuilder.setMessage("Do you feel heartburn:");


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

    }

    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);


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

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (leftArm.isChecked()) { 
                    Intent intent = new Intent(getApplicationContext(), RuleBasedActivity.class);
                    intent.putExtra("leftArm", true);
                }

                else if (bothArms.isChecked()) {
                    Intent intent = new Intent(getApplicationContext(), RuleBasedActivity.class);
                    intent.putExtra("bothArms", true);
                }

                else if (NoneArm.isChecked(){
                    Intent intent = new Intent(getApplicationContext(), RuleBasedActivity.class);
                    intent.putExtra("NoneArm", true);
                }

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

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

    }



    @Override
    public void onClick(View view) {
        if (view == buttonHome) {
            startActivity( new Intent(this, Page1Activity.class));
        }
        //check My Symptoms activity
        if (view == buttonBackBody) {
            startActivity(new Intent(this, TwoDActivity2.class));
        }
        if (view == buttonNext) {
            startActivity(new Intent(this, RuleBasedActivity.class));
        }
    }
}

RuleBasedActivity.java

    boolean highRiskOfHeartDisease, lowRiskOfHeartDisease;
    private Button diagnose;

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

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

        diagnose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Boolean leftArm = getIntent().getExtras().getBoolean("leftArm");
                Boolean bothArms = getIntent().getExtras().getBoolean("bothArms");
                Boolean NoneArm = getIntent().getExtras().getBoolean("NoneArm");
                Boolean heartburn = getIntent().getExtras().getBoolean("heartburn");
                Boolean throatOrNeck = getIntent().getExtras().getBoolean("throatOrNeck");
                Boolean jaw = getIntent().getExtras().getBoolean("jaw");
                Boolean backOfHeadAndNeck = getIntent().getExtras().getBoolean("backOfHeadAndNeck");
                Boolean NoneHead = getIntent().getExtras().getBoolean("NoneHead");

                highRiskOfHeartDisease = true;
                lowRiskOfHeartDisease = true;

                if (leftArm || bothArms) {
                    highRiskOfHeartDisease = true;
                }
                else {
                    highRiskOfHeartDisease = false;
                }

                if (NoneArm) {
                    lowRiskOfHeartDisease = true;
                }
                else {
                    lowRiskOfHeartDisease = false;
                }

                if (heartburn) {
                    lowRiskOfHeartDisease = true;
                }
                else {
                    lowRiskOfHeartDisease = false;
                }

                if (NoneArm && heartburn) {
                    highRiskOfHeartDisease = true;
                }
                else {
                    highRiskOfHeartDisease = false;
                }

                if (throatOrNeck && jaw) {
                    highRiskOfHeartDisease = true;
                }
                else {
                    highRiskOfHeartDisease = false;
                }

                if (backOfHeadAndNeck && NoneHead) {
                    highRiskOfHeartDisease = true;
                }
                else {
                    highRiskOfHeartDisease = false;
                }

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

    }
}

Noted: i'm not fully code all the list of checkbox in dialog box at the head button, shoulder button etc in the 2d image, because now i'm testing to pass/save checkbox data between 3 dialog box to RuleBasedActivity.java.




Aucun commentaire:

Enregistrer un commentaire