Sharepreference to store int value

倾然丶 夕夏残阳落幕 提交于 2020-06-26 12:13:12

问题


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;

in the oncreate

initA();
private void initA(){
if(counter < 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();
         counter = sp.getInt("counterValue", Context.MODE_PRIVATE);
        Log.i("TAG", "counter = "+counter);
    }

回答1:


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


来源:https://stackoverflow.com/questions/62587076/sharepreference-to-store-int-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!