Java 共享首选项无法获取数据
当用户在Java 共享首选项无法获取数据,java,android,sharedpreferences,decrement,Java,Android,Sharedpreferences,Decrement,当用户在EditText中给出正确的值时,我正在尝试为我的应用程序生成促销代码。我将设置一个布尔值false,并在共享首选项中存储一个数字。但是我不知道为什么这个值没有递减,即使它减小了,即使我多次这样做,它也只会从30移动到29。 因此,我创建了一个测试应用程序,在其中设置onClick中的值,当促销代码相等时会发生什么。因此,当用户打开应用程序时,当布尔值为false时,数字将减少并存储回来 public class MainActivity extends AppCompatActivit
EditText
中给出正确的值时,我正在尝试为我的应用程序生成促销代码。我将设置一个布尔值false
,并在共享首选项中存储一个数字。但是我不知道为什么这个值没有递减,即使它减小了,即使我多次这样做,它也只会从30移动到29。
因此,我创建了一个测试应用程序,在其中设置onClick
中的值,当促销代码相等时会发生什么。因此,当用户打开应用程序时,当布尔值为false时,数字将减少并存储回来
public class MainActivity extends AppCompatActivity {
TextView textView;
Button button, button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
button1 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences prefs = getSharedPreferences("Admob", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("ShowAd", false);
editor.putLong("daycounter", 30);
editor.commit();
}
});
caller();
}
@Override
protected void onResume() {
super.onResume();
caller();
}
@Override
protected void onPause() {
super.onPause();
caller();
}
public void caller() {
SharedPreferences settings = getSharedPreferences("Admob", 0);
boolean ad_start = settings.getBoolean("ShowAd", true);
long ad = settings.getLong("daycounter", 0);
SharedPreferences prefs = getSharedPreferences("apprater", 0);
SharedPreferences.Editor editor = prefs.edit();
Log.e("toaster", "" + ad_start);
if (!ad_start) {
long ads = ad -1 ;
if (ad > 0) {
editor.putLong("daycounter", ads);
editor.putBoolean("ShowAd", true);
editor.commit();
Toast.makeText(MainActivity.this, "" + ads, Toast.LENGTH_SHORT).show();
textView.setText(ad + "R" + ad_start);
}
}
}
}
在虚拟应用程序中,我在
TextView
中显示数据,因此我调用onResume
和onPause
方法,否则我知道onCreate
就足够了。我不明白算法出了什么问题。我没有得到任何错误,在吐司中,我只能将值减小到29。我已经尝试过所有类型的减量操作。任何建议都会有帮助。**我只能保存数据,问题是lonng值没有保存,也没有递减**将这些方法放在您的utils类中并使用
public static void cacheBoolean(Context ctx, String k, Boolean v) {
SharedPreferences prefs = getSharedPreferences(ctx);
prefs.edit().putBoolean(k, v).apply();
}
public static Boolean getCachedBoolean(Context ctx, String k, Boolean defaultValue) {
SharedPreferences prefs = getSharedPreferences(ctx);
return prefs.getBoolean(k, defaultValue);
}
public static void cacheString(Context ctx, String k, String v) {
SharedPreferences prefs = getSharedPreferences(ctx);
prefs.edit().putString(k, v).apply();
}
public static String getCachedString(Context ctx, String k, String defaultValue) {
SharedPreferences prefs = getSharedPreferences(ctx);
return prefs.getString(k, defaultValue);
}
public static void cacheInt(Context ctx, String k, int v) {
SharedPreferences prefs = getSharedPreferences(ctx);
prefs.edit().putInt(k, v).apply();
}
public static int getCachedInt(Context ctx, String k, int defaultValue) {
SharedPreferences prefs = getSharedPreferences(ctx);
return prefs.getInt(k, defaultValue);
}
public static void clearCachedKey(Context context, String key) {
getSharedPreferences(context).edit().remove(key).apply();
}
将这些方法放在utils类中并使用
public static void cacheBoolean(Context ctx, String k, Boolean v) {
SharedPreferences prefs = getSharedPreferences(ctx);
prefs.edit().putBoolean(k, v).apply();
}
public static Boolean getCachedBoolean(Context ctx, String k, Boolean defaultValue) {
SharedPreferences prefs = getSharedPreferences(ctx);
return prefs.getBoolean(k, defaultValue);
}
public static void cacheString(Context ctx, String k, String v) {
SharedPreferences prefs = getSharedPreferences(ctx);
prefs.edit().putString(k, v).apply();
}
public static String getCachedString(Context ctx, String k, String defaultValue) {
SharedPreferences prefs = getSharedPreferences(ctx);
return prefs.getString(k, defaultValue);
}
public static void cacheInt(Context ctx, String k, int v) {
SharedPreferences prefs = getSharedPreferences(ctx);
prefs.edit().putInt(k, v).apply();
}
public static int getCachedInt(Context ctx, String k, int defaultValue) {
SharedPreferences prefs = getSharedPreferences(ctx);
return prefs.getInt(k, defaultValue);
}
public static void clearCachedKey(Context context, String key) {
getSharedPreferences(context).edit().remove(key).apply();
}
我在这个方法中犯了一个错误,我的错误是将thx部分的值设置为
if(ad>0)
因此,当值大于零时,布尔值为true,并且数据永远不会有机会进一步递减
public void caller() {
SharedPreferences settings = getSharedPreferences("Admob", 0);
boolean ad_start = settings.getBoolean("ShowAd", true);
long ad = settings.getLong("daycounter", 0);
Log.e("toaster", "" + ad_start);
if (!ad_start) {
SharedPreferences prefs = getSharedPreferences("Admob", 0);
SharedPreferences.Editor editor = prefs.edit();
long ads = ad - 1;
if (ad < 0) {
editor.putBoolean("ShowAd", true);
}
editor.putLong("daycounter", ads);
editor.commit();
Toast.makeText(MainActivity.this, "" + ads, Toast.LENGTH_SHORT).show();
textView.setText(ad + "R" + ad_start);
}
}
public void caller(){
SharedReferences设置=获取SharedReferences(“Admob”,0);
boolean ad_start=settings.getBoolean(“ShowAd”,true);
long ad=settings.getLong(“daycounter”,0);
Log.e(“烤面包机”,“ad_启动”);
如果(!ad_start){
SharedReferences prefs=getSharedReferences(“Admob”,0);
SharedReferences.Editor=prefs.edit();
长广告=广告-1;
if(ad<0){
编辑器.putBoolean(“ShowAd”,true);
}
编辑:putLong(“daycounter”,广告);
commit();
Toast.makeText(MainActivity.this,“+ads,Toast.LENGTH_SHORT.show();
textView.setText(ad+“R”+ad_开始);
}
}
我在这个方法中犯了一个错误,对Ahmed先生来说,我的错误是将值设置为if(ad>0)
因此,当值大于零时,布尔值为true,并且数据永远不会有机会进一步递减
public void caller() {
SharedPreferences settings = getSharedPreferences("Admob", 0);
boolean ad_start = settings.getBoolean("ShowAd", true);
long ad = settings.getLong("daycounter", 0);
Log.e("toaster", "" + ad_start);
if (!ad_start) {
SharedPreferences prefs = getSharedPreferences("Admob", 0);
SharedPreferences.Editor editor = prefs.edit();
long ads = ad - 1;
if (ad < 0) {
editor.putBoolean("ShowAd", true);
}
editor.putLong("daycounter", ads);
editor.commit();
Toast.makeText(MainActivity.this, "" + ads, Toast.LENGTH_SHORT).show();
textView.setText(ad + "R" + ad_start);
}
}
public void caller(){
SharedReferences设置=获取SharedReferences(“Admob”,0);
boolean ad_start=settings.getBoolean(“ShowAd”,true);
long ad=settings.getLong(“daycounter”,0);
Log.e(“烤面包机”,“ad_启动”);
如果(!ad_start){
SharedReferences prefs=getSharedReferences(“Admob”,0);
SharedReferences.Editor=prefs.edit();
长广告=广告-1;
if(ad<0){
编辑器.putBoolean(“ShowAd”,true);
}
编辑:putLong(“daycounter”,广告);
commit();
Toast.makeText(MainActivity.this,“+ads,Toast.LENGTH_SHORT.show();
textView.setText(ad+“R”+ad_开始);
}
}
您不需要将共享首选项放在任何地方,只需在onCreate方法中对其进行初始化并从常量字符串进行访问,就可以轻松访问它
public class MainActivity extends AppCompatActivity {
TextView textView;
Button button, button1;
SharedPreferences prefs;
SharedPreferences.Editor editor;
final String pref_name = "Admob";
final String ad_name = "ShowAd";
final String day_count = "daycounter";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
button1 = (Button) findViewById(R.id.button2);
prefs = getSharedPreferences(pref_name, 0);
editor = prefs.edit();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editor.putBoolean(ad_name, false);
editor.putLong(day_count, 30);
editor.commit();
}
});
caller();
}
@Override
protected void onResume() {
super.onResume();
caller();
}
@Override
protected void onPause() {
super.onPause();
caller();
}
public void caller() {
boolean ad_start = prefs.getBoolean(ad_name, true);
long ad = prefs.getLong(day_count, 0);
Log.e("toaster", "" + ad_start);
if (!ad_start) {
long ads = ad -1 ;
if (ad < 0) {
editor.putBoolean(ad_name, true);
}
editor.putLong(day_count, ads);
editor.commit();
Toast.makeText(MainActivity.this, "" + ads, Toast.LENGTH_SHORT).show();
textView.setText(ad + "R" + ad_start);
}
}
}
public类MainActivity扩展了AppCompatActivity{
文本视图文本视图;
按钮,按钮1;
共享引用优先权;
SharedReferences.Editor;
最终字符串pref_name=“Admob”;
最终字符串ad_name=“ShowAd”;
最终字符串day\u count=“daycounter”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(textView)findViewById(R.id.textView);
按钮=(按钮)findViewById(R.id.button);
button1=(按钮)findViewById(R.id.button2);
prefs=getSharedReferences(pref_名称,0);
editor=prefs.edit();
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
编辑器.putBoolean(ad_name,false);
编辑:putLong(第30天);
commit();
}
});
调用者();
}
@凌驾
受保护的void onResume(){
super.onResume();
调用者();
}
@凌驾
受保护的void onPause(){
super.onPause();
调用者();
}
公共无效调用方(){
boolean ad_start=prefs.getBoolean(ad_name,true);
long ad=prefs.getLong(天计数,0);
Log.e(“烤面包机”,“ad_启动”);
如果(!ad_start){
长广告=广告-1;
if(ad<0){
编辑器.putBoolean(ad_name,true);
}
编辑:putLong(天数,广告);
commit();
Toast.makeText(MainActivity.this,“+ads,Toast.LENGTH_SHORT.show();
textView.setText(ad+“R”+ad_开始);
}
}
}
您不需要将共享首选项放在任何地方,只需在onCreate方法中对其进行初始化并从常量字符串进行访问,就可以轻松访问它
public class MainActivity extends AppCompatActivity {
TextView textView;
Button button, button1;
SharedPreferences prefs;
SharedPreferences.Editor editor;
final String pref_name = "Admob";
final String ad_name = "ShowAd";
final String day_count = "daycounter";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
button1 = (Button) findViewById(R.id.button2);
prefs = getSharedPreferences(pref_name, 0);
editor = prefs.edit();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editor.putBoolean(ad_name, false);
editor.putLong(day_count, 30);
editor.commit();
}
});
caller();
}
@Override
protected void onResume() {
super.onResume();
caller();
}
@Override
protected void onPause() {
super.onPause();
caller();
}
public void caller() {
boolean ad_start = prefs.getBoolean(ad_name, true);
long ad = prefs.getLong(day_count, 0);
Log.e("toaster", "" + ad_start);
if (!ad_start) {
long ads = ad -1 ;
if (ad < 0) {
editor.putBoolean(ad_name, true);
}
editor.putLong(day_count, ads);
editor.commit();
Toast.makeText(MainActivity.this, "" + ads, Toast.LENGTH_SHORT).show();
textView.setText(ad + "R" + ad_start);
}
}
}
public类MainActivity扩展了AppCompatActivity{
文本视图文本视图;
按钮,按钮1;
共享引用优先权;
SharedReferences.Editor;
最终字符串pref_name=“Admob”;
最终字符串ad_name=“ShowAd”;
最终字符串day\u count=“daycounter”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(textView)findViewById(R.id.textView);
按钮=(按钮)findViewById(R.id.button);
button1=(按钮)findViewById(R.id.button2);
prefs=getSharedReferences(pref_名称,0);
editor=prefs.edit();
按钮集