gpt4 book ai didi

java - 使用 SharedPreferences 保存字符串值,但由于某种原因未保存或由于其他问题

转载 作者:行者123 更新时间:2023-12-01 09:14:53 25 4
gpt4 key购买 nike

我正在尝试使用 Latlng 保存标记并使用 SharedPreferences 保存两个字符串,并且我正在执行一个 for 循环以在 Activity 再次午餐时检索它,我之前还有另一个问题,因为我不能' t 从 SharedPreferences 中删除标记所以我很想念我现在不知道问题是什么,请检查下面的代码以及任何可以帮助我的建议

我正在使用 et 字符串,因此我将其与标记标题相匹配,在单击标记时,我根据其标题从共享首选项中删除标记,并且已经在 onCreated 方法中初始化了共享首选项,如下所示 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 循环也正常工作");工作得很好,但是 String String iCheck = SharedPrefs.getString("Forlooper"+Integer.toString((Int)),"");当我出于某种原因提取信息时无法正常工作,并且我无法弄清楚问题是什么,非常感谢您提供任何帮助,非常感谢

最佳答案

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

editor.apply();

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

关于java - 使用 SharedPreferences 保存字符串值,但由于某种原因未保存或由于其他问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40630913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com