我正在尝试为我的应用程序制作促销代码,当用户在 EditText
中给出正确的值时,我将设置一个 boolean 值false
,并且一个数字将存储在共享首选项中。但不知道为什么该值没有减少,即使它减少了,即使我多次这样做,它也只会从 30 移动到 29。
因此,我创建了一个测试应用程序,在其中设置 onClick
中的值,当促销代码相等时会发生什么情况。所以递减的事情是当用户打开应用程序时,数字会减少并在 boolean 值为 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
就足够了。我不明白算法有什么问题。我根本没有收到任何错误,在 toast 中我只能将值减少到 29 。我尝试过所有类型的减量操作。任何建议都会有帮助。**我能够保存数据,唯一的问题是 longng 值没有被保存并且没有减少 **
最佳答案
将这些方法放入您的 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();
}
关于java - 共享首选项无法获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55128504/