0

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

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

这是这一切都发生的活动。编辑:这是我到目前为止的代码(还有更多其他 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();
}   

}

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

4

2 回答 2

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();

        }});
于 2013-07-31T21:24:49.807 回答
0

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();
    }
}   
于 2013-07-31T21:34:44.123 回答