【问题标题】:Problems with SharedPreferencesSharedPreferences 的问题
【发布时间】:2013-08-01 14:36:13
【问题描述】:

我一直试图让它工作很长时间,我已经检查了我能找到的每一个问题,因为这并没有给我任何错误,我没有太多工作要做!基本上,我有一个向 int 添加一个的按钮,该按钮显示在它旁边的 TextView 中。我希望这个 int 在应用程序关闭时保留,所以看起来 sharedpreferences 是我最好的选择(我也考虑过 sqlite 但我不确定是否可以在不更新应用程序的情况下对其进行编辑,这看起来更容易)。

所以,我的问题是 int 会很好地增加,但当我按下后退按钮/关闭应用程序等时会重置为 0。

这是发生这一切的活动。编辑:这是我到目前为止的代码(还有更多 else ifs)

package com.example.myapp;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class ThirdScreenActivity extends Activity {

    public int intPlusOne;
    public int intPlusOne2;
    public static final String PREFS = "MyPrefsFile";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);

        //Sets the title to recipe name
        TextView t = ((TextView)findViewById(R.id.textviewPosition));

        Intent intent = getIntent();
        String position = intent.getStringExtra("position");

        t.setText(position);       

        //Sets the correct recipe
    if ("ListView Item 1".equals(position)) {

        TextView t1 = ((TextView)findViewById(R.id.TextView1));
        t1.setText(R.string.string1, null); 

        SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
        intPlusOne = sharedPrefs.getInt("intPlusOneSaved", 0);

        TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
        PlusOne.setText(String.valueOf(intPlusOne));

        Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
        PlusOneButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                intPlusOne++;

            TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
            PlusOne.setText(String.valueOf(intPlusOne));            
            }
        });
    }

    else if ("ListView Item 2".equals(position)) {

        TextView t1= ((TextView)findViewById(R.id.TextView1));
        t1.setText(R.string.string2, null); 

        SharedPreferences sharedPrefs = this.getSharedPreferences("PREFS", MODE_PRIVATE);
        intPlusOne2 = sharedPrefs.getInt("intPlusOne2Saved", 0);

        TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
        PlusOne.setText(String.valueOf(intPlusOne2));

        Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
        PlusOneButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            intPlusOne2++;

            TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
            PlusOne.setText(String.valueOf(intPlusOne2));           
            }
        }); 
    }

   public void onPause() {
        super.onPause();

        SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
        Editor editor = sharedPrefs.edit();
        editor.putInt("intPlusOneSaved", intPlusOne);
        editor.commit(); //also tried removing this line
        editor.putInt("intPlusOne2Saved", IntPlusOne2);
        editor.commit();
}   

}

如果有帮助,我很乐意提供更多详细信息!

【问题讨论】:

    标签: android int sharedpreferences


    【解决方案1】:

    这很简单 你犯了一个大错误。

        String PlusOneString = String.valueOf(intPlusOne);
        Editor editor = sharedPrefs.edit();
        editor.putString("PlusOneSaved", PlusOneString);
        editor.commit();
    

    此代码在 onCreate 中,因此它仅在应用程序启动时保存该值,当该值增加时,首选项永远不会更新

    解决方案:增加值时更新首选项

        SharedPreferences sharedPrefs;//create it outside on create for accessing in on click event
    
    
        PlusOneButton.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
            intPlusOne++;
    
            TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
            PlusOne.setText(String.valueOf(intPlusOne));
    
            /// IMPORTANT PART SAVE THE VALUE TO PREFERENCES
            String PlusOneString = String.valueOf(intPlusOne);
            Editor editor = sharedPrefs.edit();
            editor.putString("PlusOneSaved", PlusOneString);
            editor.commit();
    
            }});
    

    【讨论】:

    • 你还可以使用“putInt & getInt”而不是“putString & getString”
    • 我试过 getInt 但没有成功,大概我也搞砸了!那么最后的编辑器行会进入 onClick 内部吗? (感谢您的快速回复)
    • 好的,我试过了,当我在应用程序中切换页面时效果很好。但是,如果我关闭应用程序(通过按第一页上的后退按钮),它似乎会重置它,并且当我返回时它会返回 0!
    • 尝试记录 sharedPrefs.getInt("PlusOneSaved", 0); 的值并检查是否保存,如果保存正确,则您的活动未在应用程序启动时读取值。每次更改值和首选项后,您都可以使用日志检查代码。我也可以知道你为什么使用这个 if 代码 - if ("ListView Item 1".equals(position))
    • 该代码是因为前一个活动是一个列表视图,这是您单击项目时出现的页面。将标题和其他功能设置为单击时的正确值,因为我在活动中有其他“else if”语句,但我删除了它们以便更容易回答这个问题。回到这个问题,Addrallyn 的回答完全解决了我的问题,所以我会将他的答案标记为答案,不过感谢您的快速回复和帮助,如果我继续关注它,它可能会奏效。
    【解决方案2】:

    onDestroy 并不总是在活动结束时调用。您可以尝试将您的代码放在onPause 方法中:

    public void onPause() {
        super.onPause();
    
        String PlusOneString = String.valueOf(intPlusOne);
        SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
        Editor editor = sharedPrefs.edit();
        editor.putString("PlusOneSaved", PlusOneString);
        editor.commit();
    
    }
    

    编辑:

    您不会在onCreate 方法中获得保存的值。试试这样的:

    package com.example.myapp;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    
    public class ThirdScreenActivity extends Activity {
    
        public int intPlusOne;
        public static final String PREFS = "MyPrefsFile";
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.screen2);
    
        if ("ListView Item 1".equals(position)) {
    
            TextView tv = ((TextView)findViewById(R.id.TextView1));
            tv.setText(R.string.name, null); 
    
            SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
            intPlusOne = sharedPrefs.getInt("PlusOneSaved", 0);
    
            TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
            PlusOne.setText(String.valueOf(intPlusOne));
    
            Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
            PlusOneButton.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                intPlusOne++;
    
                TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
                PlusOne.setText(String.valueOf(intPlusOne));            
                }
            });
        }
    
    public void onPause() {
            super.onPause();
    
            SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
            Editor editor = sharedPrefs.edit();
            editor.putInt("PlusOneSaved", intPlusOne);
            editor.commit();
    
        }   
    }
    

    编辑 2:将这两行放在第一个 if 之前:

    intPlusOne = sharedPrefs.getInt("intPlusOneSaved", 0);
    intPlusOne2 = sharedPrefs.getInt("intPlusOne2Saved", 0); 
    

    完整代码:

    package com.example.myapp;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    
    public class ThirdScreenActivity extends Activity {
    
    public int intPlusOne;
    public int intPlusOne2;
    public static final String PREFS = "MyPrefsFile";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);
    
        //Sets the title to recipe name
        TextView t = ((TextView)findViewById(R.id.textviewPosition));
    
        Intent intent = getIntent();
        String position = intent.getStringExtra("position");
    
        t.setText(position);    
    
        SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
        intPlusOne = sharedPrefs.getInt("intPlusOneSaved", 0);
        intPlusOne2 = sharedPrefs.getInt("intPlusOne2Saved", 0); 
    
        //Sets the correct recipe
        if ("ListView Item 1".equals(position)) {
    
            TextView t1 = ((TextView)findViewById(R.id.TextView1));
            t1.setText(R.string.string1, null); 
    
            TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
            PlusOne.setText(String.valueOf(intPlusOne));
    
            Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
            PlusOneButton.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    intPlusOne++;
    
                    TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
                    PlusOne.setText(String.valueOf(intPlusOne));            
                }
            });
        }
    
        else if ("ListView Item 2".equals(position)) {
    
            TextView t1= ((TextView)findViewById(R.id.TextView1));
            t1.setText(R.string.string2, null); 
    
            TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
            PlusOne.setText(String.valueOf(intPlusOne2));
    
            Button PlusOneButton = ((Button)findViewById(R.id.plusonebutton));
            PlusOneButton.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    intPlusOne2++;
    
                    TextView PlusOne = ((TextView)findViewById(R.id.plusonetextview));
                    PlusOne.setText(String.valueOf(intPlusOne2));           
                }
            }); 
        }
    
        public void onPause() {
            super.onPause();
    
            SharedPreferences sharedPrefs = this.getSharedPreferences(PREFS, MODE_PRIVATE);
            Editor editor = sharedPrefs.edit();
            editor.putInt("intPlusOneSaved", intPlusOne);
            editor.putInt("intPlusOne2Saved", IntPlusOne2);
            editor.commit();
        }
    }   
    

    【讨论】:

    • 非常感谢,您已经解决了!我不确定是 onDestroy/onPause 修复还是其他问题,但我花了很多时间在这上面,非常感谢。
    • 您没有在 onCreate 方法中初始化 intPlusOne 属性。
    • 嗯,我还有一个小问题,但我不确定是否要问另一个问题,或者它是否可以快速解决。我有多个“如果 x 等于位置”(使用 else if)并且它们每个都可以正常工作,即使应用程序关闭,但是如果我 +1 一个项目,返回并 +1 另一个,第一个重置为0!你能帮忙吗? (见编辑过的问题)
    • 你能发布你的else if吗?
    • 这是if 的直接复制和粘贴,只是更改了变量名。无论如何,我都会将其编辑到问题中
    猜你喜欢
    • 2021-01-03
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多