通过SharedPreferences保存值并编辑它们的问题

时间:2017-03-25 16:00:34

标签: android sharedpreferences

我将值(整数)保存到SharedPreferences,但是当我调用它们将它们应用于某些数字操作时,我发现结果为0(默认值为0)

另外,当我从最后一个Activity返回到第一个Activity并选择旁边的RadioButton时,我发现结果还可以!

那么问题是什么?

MainActivity(它只是下一个“第一页”的按钮)

第一页:

buttonp21.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        RadioGroup radGrp = (RadioGroup) findViewById(R.id.RadioGroupp2);
        int checkedRadioButtonID = radGrp.getCheckedRadioButtonId();
        radGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int id) {

                switch (id) {
                    case R.id.s4:
                        SharedPreferences sharedPreferences4 = getSharedPreferences("Data",MODE_PRIVATE);
                        SharedPreferences.Editor editor4 = sharedPreferences4.edit();
                        editor4.putInt("KEY_POINTS2", 1);
                        editor4.apply();
                        break;
                    case R.id.s5:
                        SharedPreferences sharedPreferences5 = getSharedPreferences("Data",MODE_PRIVATE);
                        SharedPreferences.Editor editor5 = sharedPreferences5.edit();
                        editor5.putInt("KEY_POINTS2", 2);
                        editor5.apply();
                        break;
                    case R.id.s6:
                        SharedPreferences sharedPreferences6 = getSharedPreferences("Data",MODE_PRIVATE);
                        SharedPreferences.Editor editor6 = sharedPreferences6.edit();
                        editor6.putInt("KEY_POINTS2", 3);
                        editor6.apply();
                        break;
                }


            }
        });
        if(checkedRadioButtonID == -1){
            Toast.makeText(getApplicationContext(), "please choose", Toast.LENGTH_SHORT).show();}
        else {
            Intent i1 = new Intent(Page1.this, Page2.class);
            startActivity(i1);
        }

    }
});

buttonp22.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Intent i1 = new Intent(Page1.this, MaineActivity.class);
        startActivity(i1);

    }
});

接下来存在20个活动,例如“First Page”....(在按钮,RadioButtons,GroupButtons,页面名称和键的id上只是不同)

最后一页:

infoo.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int DEFAULT = 0;
        SharedPreferences sharedPreferences = getSharedPreferences("Data", Context.MODE_PRIVATE);
        int p1 = sharedPreferences.getInt("KEY1", DEFAULT);  
        int p2 = sharedPreferences.getInt("KEY2", DEFAULT);
        int p3 = sharedPreferences.getInt("KEY3", DEFAULT);  
        int p4 = sharedPreferences.getInt("KEY4", DEFAULT);  
        int p5 = sharedPreferences.getInt("KEY5", DEFAULT);  
        int p6 = sharedPreferences.getInt("KEY6", DEFAULT);  
        int p7 = sharedPreferences.getInt("KEY7", DEFAULT); 
        int p8 = sharedPreferences.getInt("KEY8", DEFAULT);  
        int p9 = sharedPreferences.getInt("KEY9", DEFAULT); 
        int p10 = sharedPreferences.getInt("KEY10", DEFAULT); 
        int p11 = sharedPreferences.getInt("KEY12", DEFAULT); 
        int p11 = sharedPreferences.getInt("KEY12", DEFAULT); 
        int p13 = sharedPreferences.getInt("KEY13", DEFAULT);
        int p14 = sharedPreferences.getInt("KEY14", DEFAULT); 
        int p15 = sharedPreferences.getInt("KEY15", DEFAULT); 
        int p16 = sharedPreferences.getInt("KEY16", DEFAULT); 
        int p17 = sharedPreferences.getInt("KEY17", DEFAULT); 
        int p18 = sharedPreferences.getInt("KEY18", DEFAULT); 
        int p19 = sharedPreferences.getInt("KEY19", DEFAULT); 
        int p20 = sharedPreferences.getInt("KEY20", DEFAULT); 

        int total = p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10 + p11 + p12 + p13 + p14 + p15 + p16 + p17 + p18 + p19 + p20;
        Toast.makeText(getApplicationContext(), "" + total , Toast.LENGTH_SHORT).show();


    }
});

0 个答案:

没有答案