alanazi
alanazi

Reputation: 82

Saving a String value with SharedPreferences but for some reason it is not getting saved or due to other issue

I'm trying to save marker with Latlng and two strings with SharedPreferences and i'm doing a for loop to retrieve it when the activity is lunched again i had another question before because i couldn't delete the marker from the SharedPreferences So my mind is so missed up i can't figure it out what is the issue now please check the code below and any suggestions that could help i appreciated

I'm Using the et String so i match it to Marker title and on Marker Click i delete the Marker from sharedPreferences according to its title and already initilized the SharedPreferences in onCreated Method like so SharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

So I save the values like this,

int Int,cycler,IncreaseAmount;
String testSample;
private static final String StringLat= "Latitude",StringLng="Longitude",StringDis="Dis",StringName="Name";

String SinputNames = inputName.getText().toString();
Sinputdiscription = inputdiscription.getText().toString();


dLatitude = AddedLatLng.latitude;
dLongitude = AddedLatLng.longitude;
SharedPreferences.Editor editor = SharedPrefs.edit();

IncreaseAmount++;
editor.putInt("IJSS",IncreaseAmount);
IncreaseAmount = SharedPrefs.getInt("IJSS",0);
//SJSS = SinputNames;
//SJSS = Double.toString(dLatitude);

editor.putString("ErrorTest","Pulling up the info is working");
editor.putLong(SJSS+StringLat,Double.doubleToLongBits(dLatitude));
editor.putLong(SJSS+StringLng,Double.doubleToLongBits(dLongitude));
editor.putString(SJSS+StringName,SinputNames);
editor.putString(SJSS+StringDis,Sinputdiscription);
// editor.putString("lat"+ Integer.toString((IntCoords)), Double.toString(point.latitude));
editor.putString("ForLooper"+Integer.toString((IncreaseAmount)),SinputNames);
editor.apply();

and then pull it off from sharedPreferences

String CheckErrortest = SharedPrefs.getString("ErrorTest","Not working!");

Log.i("AlertSinput:",CheckErrortest);
cycler = SharedPrefs.getInt("IJSS",0);
if(cycler !=0) {
    String Name="";
    double Lat321,Lng321;
//     Log.i("ifCycler","if Cycler != 0 is working");
    Int = SharedPrefs.getInt("IJSS",0) +1;
    for(int i=0; i< Int ; i++ ) {
        Log.i("ForLoop:","The for loop is also working");
        // editor.putString("ForLooper"+Integer.toString((IncreaseAmount)),SJSS+StringLat);

        //Here i can't pull up the info
        String iCheck = SharedPrefs.getString("Forlooper"+Integer.toString((Int)),"");
        Log.i("TheMarkerName:","Should be"+iCheck);
        Name = SharedPrefs.getString(iCheck+StringName,"Marker");
        Lat321 = Double.longBitsToDouble(SharedPrefs.getLong(iCheck+StringLat,0));
        Lng321 = Double.longBitsToDouble(SharedPrefs.getLong(iCheck+StringLng,0));
        Log.i("Markertitle:#",Name);
        Log.i("TestTheInteger:#",Integer.toString((Int)));

        if(Lat321 !=0) {
            AddedLatLng = new LatLng(Lat321,Lng321);
            drawMarker(AddedLatLng,Name);
        }
    }
}

So the CheckErrortest and Log.i("ForLoop:","The for loop is also working"); are working Just fine but the String String iCheck = SharedPrefs.getString("Forlooper"+Integer.toString((Int)),""); Is not working when i pull up the info for some reason and i couldn't figure out what is the issue anything that could help is appreciated thank you very much

Upvotes: 0

Views: 73

Answers (2)

Kuva
Kuva

Reputation: 2437

You need to apply your changes after work with editor.

editor.commit();

or

editor.apply();

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68400

I don't see you're commiting your changes. You need to do

editor.apply();

or

editor.commit();

All changes you make in an editor are batched, and not copied back to the original SharedPreferences until you call commit() or apply()

Upvotes: 1

Related Questions