mercredi 1 février 2017

How to make SharedPreferences Condition?

How to make an optional condition with SharedPreferences? Example, like this code below. If i click "AnswerA" I will get the value of key A1_A. So,the value of key A1_B is null or 0. is it possible?

Because with the code i have below, i will get the all values. Thank you

  public void AnswerA() {
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        img1= (ImageView) findViewById(R.id.img_01);
        img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
           /*     PrefHelper.setInt(PrefKey.A1, option_scoreA1);*/
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putInt(A1_A, option_scoreA1);;
                editor.commit();
                Intent intent = new Intent(QuestionActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
    }


public void AnswerB() {
    sharedpreferences = getSharedPreferences(MyPREFERENCE, Context.MODE_PRIVATE);
    img2= (ImageView) findViewById(R.id.img_02);
    img2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
       /*     PrefHelper.setInt(PrefKey.A1, option_scoreA1);*/
            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.putInt(A1_B, option_scoreA2);;
            editor.commit();
            Intent intent = new Intent(QuestionActivity.this, SecondActivity.class);
            startActivity(intent);
        }
    });
}

Aucun commentaire:

Enregistrer un commentaire