【问题标题】:Saving a Counter to shared preferences after a button is clicked, but it doesn't save单击按钮后将计数器保存到共享首选项,但不保存
【发布时间】:2019-09-11 10:16:51
【问题描述】:

我试图让一个按钮增加一个计数器并将计数器保存到共享首选项,这样即使退出应用程序,计数器值也不会重置为 0。我尝试使用共享首选项,但值只是不会保存。我看过其他类似的形式,但无法理解如何将其实现到我的代码版本中。

代码如下:

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Screen2 extends AppCompatActivity {

    TextView showValue;

    int counter = 0;

    SharedPreferences sharedpreferences;
    public static final String MyPREFERENCES = "MyPrefs" ;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen2);

        showValue = findViewById(R.id.AnnieCount);
        //showValue.setVisibility(View.INVISIBLE);

      sharedpreferences = getSharedPreferences(MyPREFERENCES, 
      Context.MODE_PRIVATE);

        public void AnCount(View v) {
            //increase the count
            counter++;
            showValue.setText(Integer.toString(counter));
            SharedPreferences.Editor editor = sharedpreferences.edit ();
            editor.putInt("annie", Integer.valueOf(counter));
            // openDialog();

    }

编辑

这是最低限度的工作代码和 XML:

主要活动:

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView showValue;
    int counter = 0;

    SharedPreferences sharedpreferences;
    public static final String MyPREFERENCES = "MyPrefs" ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showValue = findViewById(R.id.AnnieCount);
        //showValue.setVisibility(View.INVISIBLE);

        sharedpreferences = getSharedPreferences(MyPREFERENCES,
                Context.MODE_PRIVATE);


    }


    public void AnCount(View v) {
        //increase the count
        counter++;
        showValue.setText(Integer.toString(counter));
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putInt("counter", counter);
        editor.commit();
        // openDialog();
    }
}

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/AnnieCount"
        android:layout_width="57dp"
        android:layout_height="43dp"
        android:layout_marginStart="12dp"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="145dp"
        android:layout_marginEnd="167dp"
        android:layout_marginRight="167dp"
        android:gravity="center"
        android:text="0"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/AnnieBtn"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/AnnieBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="72dp"
        android:layout_marginLeft="72dp"
        android:layout_marginTop="145dp"
        android:layout_marginEnd="12dp"
        android:layout_marginRight="12dp"
        android:onClick="AnCount"
        android:text="annie_liou"
        app:layout_constraintEnd_toStartOf="@+id/AnnieCount"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

我需要做的就是在完全关闭应用程序后存储增量值。

【问题讨论】:

    标签: android-studio sharedpreferences saving-data


    【解决方案1】:

    您应该在离开之前致电editor.commit()。在您调用commit()apply() 之前,您在SharedPreferences 编辑器中所做的所有更改都不会存储在SharedPreferences 中。您可以从here 阅读更多内容。

    以下是您可以解决的方法:

    public void AnCount(View v) {
            //increase the count
            counter++;
            showValue.setText(Integer.toString(counter));
            SharedPreferences.Editor editor = sharedpreferences.edit ();
            editor.putInt("annie", Integer.valueOf(counter));
            editor.commit();
            // openDialog();
    
    }
    

    要查看SharedPreferences的存储值,可以使用sharedpreferences.getInt("counter", 0)获取存储值:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        showValue = findViewById(R.id.AnnieCount);
        //showValue.setVisibility(View.INVISIBLE);
    
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    
        counter = sharedpreferences.getInt("counter", 0);
    
        showValue.setText("" + counter);
    }
    

    【讨论】:

    • 感谢您的帮助!我添加了 editor.commit();但似乎该值仍未存储。 editor.putInt("annie", Integer.valueOf(counter)); - 对你来说是正确的。
    • 你在哪里调用 AnCount 方法?
    • 另外,你从哪里获得 sharedpreferences 的最新值?
    • AnCount 是分配给同一 XML 中的按钮的 OnClick 方法
    • 不太清楚你所说的“sharedpreferences 的最新值”是什么意思,对不起,我对 android 编程很陌生
    猜你喜欢
    • 2012-11-14
    • 2019-10-27
    • 2016-06-06
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 2023-04-09
    相关资源
    最近更新 更多