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));
}
}
12
solved SharedPreferences Save value of Int in a TextView