共享首选项以存储int值 [英] Sharepreference to store int value

查看:59
本文介绍了共享首选项以存储int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 int 值,当我们单击警报"对话框的肯定或否定按钮时,我希望它增加1,并存储int值,即使用户关闭应用程序也是如此.我已经完成了这些操作,但是我不知道为什么这不起作用.

I have int value and I want it to increases by 1 when we click positive or negative button of Alert dialogue, and store the int value even when the user closes the app. I've done these but I don't know why is this not working.

int counter;

在oncreate

initA();

private void initA(){

if(getCounter() < 1)
{makeAlertDialogOther();}
}

private void makeAlertDialogOther() {
        new AlertDialog.Builder(getActivity())
                .setMessage(Constant.SETTINGS.getEntranceMsg())
                .setCancelable(false)
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        counterU();

                    }
                })
                .setPositiveButton("Update", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        counterU();
                    }
                })
                .show();
    }

这是我进行共享设置的地方:

This is where I made sharepreference:

 private void counterU() {
        sp = getActivity().getSharedPreferences("MyPrfs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        int oldCounter = sp.getInt("counterValue", 0);
        editor.putInt("counterValue", oldCounter + 1);
        editor.apply();
    }

private int getCounter() {
        sp = getActivity().getSharedPreferences("MyPrfs", Context.MODE_PRIVATE);
        return sp.getInt("counterValue", 0);
    }

推荐答案

代码不起作用的原因:每当您关闭并再次打开屏幕时,就开始再次将新的计数器值(从0开始)保存到您的 SharedPreferences .
解决方案:每当我们开始将计数器保存到 SharedPreferences 时,我们首先在 SharedPreferences 中获取计数器的旧值,然后增加并保存回来.

The reason your code not working: Whenever you close and open screen again, you start to save a new counter value (start from 0) again to your SharedPreferences.
The solution: Whenever we start to save a counter to SharedPreferences, we get the old value of counter in SharedPreferences first then increase and save back.

private void initA() {
     if(getCounter() < 1) {
        makeAlertDialogOther();
     }
}

private void makeAlertDialogOther() {
    new AlertDialog.Builder(getActivity())
        .setMessage(Constant.SETTINGS.getEntranceMsg())
        .setCancelable(false)
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                counterU();
            }
        })
        .setPositiveButton("Update", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                counterU();
            }
        })
        .show();
}

private void counterU() {
    sp = getActivity().getSharedPreferences("MyPrfs", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    int oldCounter = sp.getInt("counterValue", 0);
    editor.putInt("counterValue", oldCounter + 1);
    editor.apply();
}

private int getCounter() {
    sp = getActivity().getSharedPreferences("MyPrfs", Context.MODE_PRIVATE);
    return sp.getInt("counterValue", 0);
}

这篇关于共享首选项以存储int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆