Sharepreference 存储 int 值
Sharepreference to store int value
我有 int
值,我希望它在我们单击警报对话框的肯定或否定按钮时增加 1,并存储 int 值,即使用户关闭应用程序也是如此。我已经做了这些,但我不知道为什么这不起作用。
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();
}
这是我设置共享偏好的地方:
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
。
解决方法:每当我们开始存一个counter到SharedPreferences
时,先取SharedPreferences
中counter的旧值,然后增加存回。
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
值,我希望它在我们单击警报对话框的肯定或否定按钮时增加 1,并存储 int 值,即使用户关闭应用程序也是如此。我已经做了这些,但我不知道为什么这不起作用。
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();
}
这是我设置共享偏好的地方:
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
。
解决方法:每当我们开始存一个counter到SharedPreferences
时,先取SharedPreferences
中counter的旧值,然后增加存回。
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);
}