I have a main app that I want to store score integer using SharedPreferences










-1















When the app is restarted it returns the default value instead of the
previously saved value. I need some help.
The main problem here is that I have followed the steps that sharedpreferences demands, the is saved but cannot be restored on app re-open.



The default value in returned when the app is re opened ie
Score=myScore.getInt ( "score",0 );



Here is the sample of the score
app code.



package tips.admin.tipsscore.score;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
TextView score;
Button add,sub;
Context context;
int Score=0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
score=(TextView)findViewById ( R.id.score );
add=(Button)findViewById ( R.id.increase );
sub=(Button)findViewById ( R.id.decrease );
SharedPreferences myScore=context.getSharedPreferences ("myscore", Context.MODE_PRIVATE);
Score=myScore.getInt ( "score",0 );
score.setText ( "SCORE : "+Score );
add.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score+=45;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );


);
sub.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score-=21;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );

);












share|improve this question



















  • 1





    Your key (SCORE : & score) is different while save and retrieve your score.

    – Piyush
    Nov 15 '18 at 11:31












  • the key, at save time you used "SCORE : " but at get time you used "score". The key must be same and also it is case sensitive..

    – user392117
    Nov 15 '18 at 11:34











  • Just to clarify Piyush comment: you are saving a value under "SCORE : " key, but retrieving a value under "score" key

    – Vladyslav Matviienko
    Nov 15 '18 at 11:35











  • SCORE : & score , i never thought of that.thank you peoples

    – joseph
    Nov 15 '18 at 11:44















-1















When the app is restarted it returns the default value instead of the
previously saved value. I need some help.
The main problem here is that I have followed the steps that sharedpreferences demands, the is saved but cannot be restored on app re-open.



The default value in returned when the app is re opened ie
Score=myScore.getInt ( "score",0 );



Here is the sample of the score
app code.



package tips.admin.tipsscore.score;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
TextView score;
Button add,sub;
Context context;
int Score=0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
score=(TextView)findViewById ( R.id.score );
add=(Button)findViewById ( R.id.increase );
sub=(Button)findViewById ( R.id.decrease );
SharedPreferences myScore=context.getSharedPreferences ("myscore", Context.MODE_PRIVATE);
Score=myScore.getInt ( "score",0 );
score.setText ( "SCORE : "+Score );
add.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score+=45;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );


);
sub.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score-=21;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );

);












share|improve this question



















  • 1





    Your key (SCORE : & score) is different while save and retrieve your score.

    – Piyush
    Nov 15 '18 at 11:31












  • the key, at save time you used "SCORE : " but at get time you used "score". The key must be same and also it is case sensitive..

    – user392117
    Nov 15 '18 at 11:34











  • Just to clarify Piyush comment: you are saving a value under "SCORE : " key, but retrieving a value under "score" key

    – Vladyslav Matviienko
    Nov 15 '18 at 11:35











  • SCORE : & score , i never thought of that.thank you peoples

    – joseph
    Nov 15 '18 at 11:44













-1












-1








-1


1






When the app is restarted it returns the default value instead of the
previously saved value. I need some help.
The main problem here is that I have followed the steps that sharedpreferences demands, the is saved but cannot be restored on app re-open.



The default value in returned when the app is re opened ie
Score=myScore.getInt ( "score",0 );



Here is the sample of the score
app code.



package tips.admin.tipsscore.score;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
TextView score;
Button add,sub;
Context context;
int Score=0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
score=(TextView)findViewById ( R.id.score );
add=(Button)findViewById ( R.id.increase );
sub=(Button)findViewById ( R.id.decrease );
SharedPreferences myScore=context.getSharedPreferences ("myscore", Context.MODE_PRIVATE);
Score=myScore.getInt ( "score",0 );
score.setText ( "SCORE : "+Score );
add.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score+=45;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );


);
sub.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score-=21;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );

);












share|improve this question
















When the app is restarted it returns the default value instead of the
previously saved value. I need some help.
The main problem here is that I have followed the steps that sharedpreferences demands, the is saved but cannot be restored on app re-open.



The default value in returned when the app is re opened ie
Score=myScore.getInt ( "score",0 );



Here is the sample of the score
app code.



package tips.admin.tipsscore.score;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
TextView score;
Button add,sub;
Context context;
int Score=0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
score=(TextView)findViewById ( R.id.score );
add=(Button)findViewById ( R.id.increase );
sub=(Button)findViewById ( R.id.decrease );
SharedPreferences myScore=context.getSharedPreferences ("myscore", Context.MODE_PRIVATE);
Score=myScore.getInt ( "score",0 );
score.setText ( "SCORE : "+Score );
add.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score+=45;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );


);
sub.setOnClickListener ( new View.OnClickListener ()
@Override
public void onClick(View v)
Score-=21;
SharedPreferences sharedPreferences=getSharedPreferences ("myscore", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= sharedPreferences.edit ();
editor.putInt ( "SCORE : ",Score);
editor.commit ();
score.setText ( "SCORE : "+Score );

);









android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 11:56









James Westgate

8,69654558




8,69654558










asked Nov 15 '18 at 11:28









josephjoseph

81




81







  • 1





    Your key (SCORE : & score) is different while save and retrieve your score.

    – Piyush
    Nov 15 '18 at 11:31












  • the key, at save time you used "SCORE : " but at get time you used "score". The key must be same and also it is case sensitive..

    – user392117
    Nov 15 '18 at 11:34











  • Just to clarify Piyush comment: you are saving a value under "SCORE : " key, but retrieving a value under "score" key

    – Vladyslav Matviienko
    Nov 15 '18 at 11:35











  • SCORE : & score , i never thought of that.thank you peoples

    – joseph
    Nov 15 '18 at 11:44












  • 1





    Your key (SCORE : & score) is different while save and retrieve your score.

    – Piyush
    Nov 15 '18 at 11:31












  • the key, at save time you used "SCORE : " but at get time you used "score". The key must be same and also it is case sensitive..

    – user392117
    Nov 15 '18 at 11:34











  • Just to clarify Piyush comment: you are saving a value under "SCORE : " key, but retrieving a value under "score" key

    – Vladyslav Matviienko
    Nov 15 '18 at 11:35











  • SCORE : & score , i never thought of that.thank you peoples

    – joseph
    Nov 15 '18 at 11:44







1




1





Your key (SCORE : & score) is different while save and retrieve your score.

– Piyush
Nov 15 '18 at 11:31






Your key (SCORE : & score) is different while save and retrieve your score.

– Piyush
Nov 15 '18 at 11:31














the key, at save time you used "SCORE : " but at get time you used "score". The key must be same and also it is case sensitive..

– user392117
Nov 15 '18 at 11:34





the key, at save time you used "SCORE : " but at get time you used "score". The key must be same and also it is case sensitive..

– user392117
Nov 15 '18 at 11:34













Just to clarify Piyush comment: you are saving a value under "SCORE : " key, but retrieving a value under "score" key

– Vladyslav Matviienko
Nov 15 '18 at 11:35





Just to clarify Piyush comment: you are saving a value under "SCORE : " key, but retrieving a value under "score" key

– Vladyslav Matviienko
Nov 15 '18 at 11:35













SCORE : & score , i never thought of that.thank you peoples

– joseph
Nov 15 '18 at 11:44





SCORE : & score , i never thought of that.thank you peoples

– joseph
Nov 15 '18 at 11:44












2 Answers
2






active

oldest

votes


















1














You using a different keys for saving and restroring value from shared preferences.
You need to use a same key.



For example:



save:



editor.putInt ("score_value", Score);



read:



myScore.getInt ("score_value", 0);





share|improve this answer






























    1














    Use string constants instead, to avoid misspelling your tags.



    In your class declare :



    private static final String NAME_OF_TAG = “name_of_tag”


    Then use that string instead:



    editor.putInt(NAME_OF_TAG, 3);

    int value = preferences.getInt(NAME_OF_TAG, 0);





    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318468%2fi-have-a-main-app-that-i-want-to-store-score-integer-using-sharedpreferences%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You using a different keys for saving and restroring value from shared preferences.
      You need to use a same key.



      For example:



      save:



      editor.putInt ("score_value", Score);



      read:



      myScore.getInt ("score_value", 0);





      share|improve this answer



























        1














        You using a different keys for saving and restroring value from shared preferences.
        You need to use a same key.



        For example:



        save:



        editor.putInt ("score_value", Score);



        read:



        myScore.getInt ("score_value", 0);





        share|improve this answer

























          1












          1








          1







          You using a different keys for saving and restroring value from shared preferences.
          You need to use a same key.



          For example:



          save:



          editor.putInt ("score_value", Score);



          read:



          myScore.getInt ("score_value", 0);





          share|improve this answer













          You using a different keys for saving and restroring value from shared preferences.
          You need to use a same key.



          For example:



          save:



          editor.putInt ("score_value", Score);



          read:



          myScore.getInt ("score_value", 0);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 11:34









          OnixOnix

          4478




          4478























              1














              Use string constants instead, to avoid misspelling your tags.



              In your class declare :



              private static final String NAME_OF_TAG = “name_of_tag”


              Then use that string instead:



              editor.putInt(NAME_OF_TAG, 3);

              int value = preferences.getInt(NAME_OF_TAG, 0);





              share|improve this answer



























                1














                Use string constants instead, to avoid misspelling your tags.



                In your class declare :



                private static final String NAME_OF_TAG = “name_of_tag”


                Then use that string instead:



                editor.putInt(NAME_OF_TAG, 3);

                int value = preferences.getInt(NAME_OF_TAG, 0);





                share|improve this answer

























                  1












                  1








                  1







                  Use string constants instead, to avoid misspelling your tags.



                  In your class declare :



                  private static final String NAME_OF_TAG = “name_of_tag”


                  Then use that string instead:



                  editor.putInt(NAME_OF_TAG, 3);

                  int value = preferences.getInt(NAME_OF_TAG, 0);





                  share|improve this answer













                  Use string constants instead, to avoid misspelling your tags.



                  In your class declare :



                  private static final String NAME_OF_TAG = “name_of_tag”


                  Then use that string instead:



                  editor.putInt(NAME_OF_TAG, 3);

                  int value = preferences.getInt(NAME_OF_TAG, 0);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 11:40









                  Nikos HidalgoNikos Hidalgo

                  1,0901317




                  1,0901317



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318468%2fi-have-a-main-app-that-i-want-to-store-score-integer-using-sharedpreferences%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線