vendredi 16 juin 2017

get value of checkbox from a fragment to another class that is not an activity

i'm trying to make a configuration page that allows the user to choose whether he wants his password to be remembered or not. But i don't know where to do the "set" and the "get" of the shared preferences and where to send the intent since the other class is not an activity.

This is my fragment :

public class configFragment extends android.support.v4.app.Fragment {

    CheckBox check;
    public static Boolean checkBoxValue;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.config_layout, container, false);
        TextView titre=(TextView) rootView.findViewById(R.id.configu);
        check=(CheckBox) rootView.findViewById(R.id.checkbox_meat);
        TextView avertissement=(TextView) rootView.findViewById(R.id.avertissement);
        titre.setText("Configuration");
        avertissement.setText("Attention ! : Le code n'est pas chiffré!");
        check();
        return rootView;
    }

    public boolean check(){
       if(check.isChecked()){
           Intent intent=new Intent(getActivity(),HomeActivity.class);
           intent.putExtra("value",true);
           return  true;
       }else{
           return false;
       }

    }
}

and this is the method in the other class:

 public void askForPassword() {
    final EditText et = new EditText(mHomeViewModel.getFragment().getActivity());
    et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    SharedPreferences loginPreferences = mHomeViewModel.getFragment().getActivity().getSharedPreferences(SPF_NAME,
            Context.MODE_PRIVATE);
    et.setText(loginPreferences.getString("password", ""));
    new AlertDialog.Builder(mHomeViewModel.getFragment().getActivity())
            .setTitle("Saisissez votre code  Le Coffre")
            .setView(et)
            .setPositiveButton("Valider", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                /* if(value==true){
                     SharedPreferences settings = mHomeViewModel.getFragment().getActivity().getSharedPreferences(PREFRENCES_NAME, 0);
                     settings.edit().putString("pwd", et.getText().toString()).commit();
                    // BsApplication.sPassword = password;
                 }else {
                     BsApplication.sPassword = et.getText().toString();

                     //}
                 }
                    initBsAndCloudAndBrowse();
                    }*/

                    BsApplication.sPassword = et.getText().toString();
                    initBsAndCloudAndBrowse();}
            })
            .setNegativeButton("Première connexion?", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                    final EditText et1 = new EditText(mHomeViewModel.getFragment().getActivity());
                    et1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    new AlertDialog.Builder(mHomeViewModel.getFragment().getActivity())
                            .setView(et1)
                            .setTitle("Première connexion")
                            .setMessage("Choisissez un code différent de celui du cloud Orange "+"\n"+"Attention nous ne connaitrons pas votre code et n’offrons pas encore la possibilité de le changer ")
                            .setPositiveButton("Créer", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    BsApplication.sPassword = et1.getText().toString();

                                    initBsAndCloudAndBrowse();

                                }
                            })
                            .show();


                }
            })
            .show();

}

Thank you for your help , i'd really appreciate it




Aucun commentaire:

Enregistrer un commentaire