簡體   English   中英

使用SharedPreferences保存一個字符串值,但是由於某種原因,它沒有被保存或由於其他問題

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

我正在嘗試使用Latlng保存標記,並使用SharedPreferences保存兩個字符串,並且我正在做一個for循環以在活動再次吃午飯時檢索它,我之前還有一個問題,因為我無法從SharedPreferences刪除該標記,所以我想太錯過了,我現在無法弄清楚問題是什么,請檢查下面的代碼以及任何可以幫助我的建議

我正在使用et String,因此我將其與Marker標題匹配,並在Marker上單擊,然后根據標題將其從sharedPreferences中刪除,並已經在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)),""); 當我出於某種原因提取信息時無法正常工作時,我不知道是什么問題,任何可以幫助的問題都值得感謝,非常感謝

我看不到您要進行更改 你需要做

editor.apply();

要么

editor.commit();

您在編輯器中所做的所有更改都將被分批處理,並且直到您調用commit()或apply()時,才將其復制回到原始的SharedPreferences中。

使用編輯器后,您需要應用更改。

editor.commit();

要么

editor.apply();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM