-2

When storing data in android studio using SharedPreferences. it store did not store the Newest value at first. I mean the data location does not update. I want to newest store data in 1st location and the previously stored data automatics shift to the next location. Example> I enter/store first time a value in Edit Text. it stores in the first location but when I store the Second value. It did not update the first value location it store data in the second location.

btnSaved.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {

               SharedPreferences.Editor editor = sharedpreferences.edit();
               editor.putInt("id", item.getId());
               editor.putString("name", item.getName());
               editor.putString("image", item.getImge());
               editor.putString("status", item.getStatus());
               editor.putString("timeStamp", item.getTimeStamp());
               editor.putString("URL", item.getUrl());
               editor.commit();
OshiniRB
  • 578
  • 1
  • 7
  • 21
ALI Alpha
  • 1
  • 2

1 Answers1

0
  1. if you are new to android studio understand data storage options clearly. this one will help.

  2. if you are going to use id like "status", "email", "name" multiple times save them in a separate class as static final variables.

  3. don't save variables like that make a separate class as User or something, set the value there, and save it as a JSON object.

like this

getAppPrefsEditor().putString("user", provideGson().toJson(object of YourClass)).apply();

and get them like this

String userStr = getAppPrefs().getString("user", null);

user = provideGson().fromJson(userStr, YourClass.class);

and you can get the value inside "user"

  1. also there is some free cloud storage if you want to try you can check, firebase is quite easy to start with.firebase web link
Dharman
  • 30,962
  • 25
  • 85
  • 135
subrata sharma
  • 344
  • 4
  • 17