-2

I try to save the value of an int displayed in a TextView using SharedPreferences, it can't work at all. I did a simple small example of code :

public class MainActivity extends Activity {

Button search;
TextView tvRing;
int redRing;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final SharedPreferences prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);

    search = (Button) findViewById(R.id.radar);
    tvRing = (TextView) findViewById(R.id.ring);

    int someint = prefs.getInt("someint", 0);
    tvRing.setText("Objects found : " + String.valueOf(someint));

    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addRing();
            SharedPreferences.Editor editor = prefs.edit();
            editor.putInt("someint", redRing);
            editor.commit();
        }
    });
}

public void addRing() {
    redRing++;
}
}

Thank u all for yo help.

Ghiggz Pikkoro
  • 136
  • 1
  • 14

2 Answers2

1

You should use

SharedPreferences.Editor editor = prefs.edit();
        editor.putInt("someint", redRing);
        editor.apply();

and you are never using "someint", use

tvRing.setText(String.valueOf(someint));
Martin De Simone
  • 2,108
  • 3
  • 16
  • 30
  • You are never using "someint" – Martin De Simone May 08 '17 at 15:21
  • place it below the line you assign someint – Martin De Simone May 08 '17 at 15:31
  • Ok it's working, the value of the int was saved in SharedPreferences but now on the TextView the value doesn't increase when I click the button Search, however if I quit the app and I come back again on my app the TextView is updated, do u know why? – Ghiggz Pikkoro May 08 '17 at 15:38
  • I made an edit of my post with yo advices, can u check what's wrong? – Ghiggz Pikkoro May 08 '17 at 15:42
  • Update this public void addRing() { redRing++; tvRing.setText(String.valueOf(redRing)); } – Hasan Saykın May 08 '17 at 16:35
  • Yes I tried it but something wrong, because if I quit the app, I just retrieve the number of click I did on the button last time I was on the app and not everytime, it's difficult to explain but it's not working as well – Ghiggz Pikkoro May 08 '17 at 16:44
  • Hi Hasan, I did a new question here : http://stackoverflow.com/questions/43859463/sharedpreferences-save-value-of-int-in-a-textview-of-another-activity Can u check where's there's the wrong code please? – Ghiggz Pikkoro May 09 '17 at 01:08
1

I explained where I made changes

public class MainActivity extends Activity {

    Button search;
    TextView tvRing;

    //Making sharedpreferences and integers global for ease of use
    private SharedPreferences prefs;
    private int redRing, someint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        search = (Button) findViewById(R.id.radar);
        tvRing = (TextView) findViewById(R.id.ring);

        //Someint default value is 0 if not ever saved before
        prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
        someint = prefs.getInt("someint", 0);
        tvRing.setText("Objects found : " + String.valueOf(someint));

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Load the lates someint in onclick
                someint = prefs.getInt("someint", 0);

                //redring is the dummy integer to increment someint
                redRing=someint+1;
                //Save the incremented value
                SharedPreferences.Editor editor = prefs.edit();
                editor.putInt("someint", redRing);
                editor.commit();

                //To show the latest number on the tv
                lastNumber();
            }
        });
    }


    public void lastNumber() {
        prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE);
        someint = prefs.getInt("someint", 0);
        tvRing.setText("Objects found : " + String.valueOf(someint));
    }
}
Hasan Saykın
  • 138
  • 1
  • 8
  • I tried it, there's save of value but the problem is if I quit the app and click again on the button search, the TextView start to 0 again but before click I can see the last number – Ghiggz Pikkoro May 08 '17 at 16:57
  • Check this out. – Hasan Saykın May 08 '17 at 17:06
  • I deleted redundant final sharedpreferences call in lastNumber method. Sorry. – Hasan Saykın May 08 '17 at 17:12
  • With this edit, app will always remember the last number in tv. And as you click on that search button the number in tv will be incremented. Is this what you want? Or you just want to start from 0 all the time? – Hasan Saykın May 08 '17 at 17:14
  • But my code isnt really like that in my app, I just simplified it, maybe I got to change something. I explain it: I have a class where I created Ring object, the addRing() method is on this class where the redRing int variable increase. Then I have an activity with the button search, I click on it and create a new Ring and I call addRing() method so the user can find some new Ring objects. Then I have another activity for display the stock of each different rings because there's different colors of rings. Do u think I got to change something in order to do the same thing but with 2 activities? – Ghiggz Pikkoro May 08 '17 at 17:25
  • You can change someint as dummy and save redRing int in sharedpreferences maybe. I can't be sure. You should try. And you can, for example load redRing int everywhere using: prefs = getSharedPreferences("sharedPreferences", Context.MODE_PRIVATE); redRing = prefs.getInt("someint", 0); Of course you have to make sharedpreferences global as well if you use like this. private SharedPreferences prefs; – Hasan Saykın May 08 '17 at 17:45
  • Yes ok but the SharedPreferences need a Context in order to work, and in my class Ring I have,'t no context because I just created the object Ring and add some methods, there's no layout because it's not an activity. Maybe I can post a new question, u can check it? – Ghiggz Pikkoro May 08 '17 at 18:28
  • I used "import static" in order to share the value of redRing to my Activity1 – Ghiggz Pikkoro May 08 '17 at 18:29
  • So, with importing is it working? If not, I would suggest you to search this website first about the issue, and if you cannot find anything then post your question. – Hasan Saykın May 08 '17 at 19:19
  • There are some posts about it. Some of them: http://stackoverflow.com/questions/33360999/getting-sharedpreferences-in-non-activity-class http://stackoverflow.com/questions/7491287/android-how-to-use-sharedpreferences-in-non-activity-class – Hasan Saykın May 08 '17 at 19:24
  • Ok I did something, it seems working, but I got to add all variables and test it again, I will do it tomorrow, if I have an issue I will post a new question about it. Thank u so much – Ghiggz Pikkoro May 08 '17 at 22:36
  • I don't know what cause it but sometimes there's a reset, the sum come back to 0 if I quit the app but not everytime, it's strange, I will post a question tomorrow maybe – Ghiggz Pikkoro May 08 '17 at 23:02