0

I am trying to use Shared Preferences in Java to store a variable so that any time I run the program it will retain its count. I am just not sure how to use it. Do I need to create the Share Preferences class or can I use it for example my code looks like this.

 if(action.equals ("insert")
 {
 int booking_id = (initially be zero);
 booking_id += 1; 
 // I want booking Id to retain its value and not become zero the next time I run it.

 }
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190

1 Answers1

0

I am assuming you mean Shared Preferences as available in Android.

You would want to ask the SharedPreferences for the value of booking_id:

SharedPreferences pref = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
int booking_id = pref.getInt("booking_id", 0); // 0 is the default value if the preferences do not find the "booking_id"
RaptorDotCpp
  • 1,425
  • 14
  • 26