2013-07-31 39 views
0

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

所以,我的问题是,当我按下后退按钮/关闭应用程序等

这里的,这是所有在EDIT发生的活动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(); 
} 

}

我很乐意提供任何更多的细节,如果有帮助!

回答

0

在活动结束时并不总是调用onDestroy。你可以尝试把你的代码在方法:

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之前把这些2线:

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(); 
    } 
} 
+0

非常感谢,你已经解决了!我不确定这是否是onDestroy/onPause修复或其他问题,但我花了很多时间在此,非常感谢。 – stackUnderflow

+0

您未在'onCreate'方法中初始化您的'intPlusOne'属性。 – Brtle

+0

嗯,我还有一个小问题,但我不确定是否要问另一个问题,或者它是否可以快速解决。我有不止一个“如果x等于位置”(使用else if),并且它们各自独立工作,即使应用程序已关闭,但如果我+1 +1项,请返回+1另一项,第一个重置为0!你能帮忙吗? (看编辑的问题) – stackUnderflow

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

     }}); 
+0

一件事,你也可以使用“putInt &getInt“而不是”putString&getString“ – Diljeet

+0

我试过getInt,它没有工作,大概我也搞砸了!那么编辑器最后会在onClick里面呢? (并感谢您的快速响应) – stackUnderflow

+0

好的,我试过这个,当我在应用中切换页面时,它完美地工作。但是,如果我关闭了应用程序(通过按第一页上的后退按钮),它似乎将其重置,并且当我返回时它回到0! – stackUnderflow