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

时间:2016-11-16 11:24:04

标签: java android sharedpreferences

我正在尝试使用Latlng保存标记和使用SharedPreferences的两个字符串,并且我正在执行for循环以在活动再次启动时检索它我之前有另一个问题因为我无法删除来自SharedPreferences的标记所以我的思绪如此错过了我无法弄清楚现在的问题是什么请检查下面的代码以及任何可以帮助我感激的建议

我正在使用et String,所以我将它与Marker title和Marker相匹配点击我根据标题从sharedPreferences中删除Marker,并已在onCreated方法中启用了SharedPreferences,如SharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); < /强>

所以我保存了这样的值,

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();

然后从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);
        }
    }
}

所以CheckErrortestLog.i("ForLoop:","The for loop is also working"); 工作正常但字符串String iCheck = SharedPrefs.getString("Forlooper"+Integer.toString((Int)),""); 当我出于某种原因提取信息时无法正常工作,我无法弄清楚什么是可以帮助的问题,非常感谢谢谢

2 个答案:

答案 0 :(得分:1)

我没有看到你commiting your changes。你需要做

editor.apply();

editor.commit();
  

您在编辑器中所做的所有更改都是批处理的,在您调用commit()或apply()

之前不会将其复制回原始的SharedPreferences

答案 1 :(得分:0)

您需要在使用编辑器后应用更改。

editor.commit();

editor.apply();