简体   繁体   English

使用SharedPreferences保存一个字符串值,但是由于某种原因,它没有被保存或由于其他问题

[英]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 我正在尝试使用Latlng保存标记,并使用SharedPreferences保存两个字符串,并且我正在做一个for循环以在活动再次吃午饭时检索它,我之前还有一个问题,因为我无法从SharedPreferences删除该标记,所以我想太错过了,我现在无法弄清楚问题是什么,请检查下面的代码以及任何可以帮助我的建议

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()); 我正在使用et String,因此我将其与Marker标题匹配,并在Marker上单击,然后根据标题将其从sharedPreferences中删除,并已经在onCreated方法中初始化了SharedPreferences,例如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 然后从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"); 因此, CheckErrortestLog.i("ForLoop:","The for loop is also working"); are working Just fine but the String String iCheck = SharedPrefs.getString("Forlooper"+Integer.toString((Int)),""); 工作正常,但字符串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 当我出于某种原因提取信息时无法正常工作时,我不知道是什么问题,任何可以帮助的问题都值得感谢,非常感谢

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() 您在编辑器中所做的所有更改都将被分批处理,并且直到您调用commit()或apply()时,才将其复制回到原始的SharedPreferences中。

You need to apply your changes after work with editor. 使用编辑器后,您需要应用更改。

editor.commit();

or 要么

editor.apply();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM