簡體   English   中英

未使用 SharedPreferences 存儲的字符串

[英]Strings not being stored using SharedPreferences

我試圖在 SharedPreferences 中保存兩個字符串。 當我第一次運行應用程序時,它們按預期顯示。 當我關閉應用程序並重新打開應用程序時,字符串不再設置為關閉應用程序之前的狀態,並且使用默認字符串值。 為什么是這樣? 下面是我保存和檢索兩個字符串的方法。

這是兩個字符串

Intent intent = getIntent();

    playerName1 = intent.getStringExtra("PLAYER_ONE");
    playerName2 = intent.getStringExtra("PLAYER_TWO");

這是存儲字符串的方法

private void savePlayerNames()
{
    SharedPreferences sharedPreferences = getSharedPreferences("playerNames", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("playerOne", playerName1);
    editor.putString("playerTwo", playerName2);
    editor.commit();
}

這是它們被檢索的地方

private void displayPlayerNames(int current)
{
    SharedPreferences sharedPreferences = getSharedPreferences("playerNames", Context.MODE_PRIVATE);

    String playerOne = sharedPreferences.getString("playerOne","Player 1");
    String playerTwo = sharedPreferences.getString("playerTwo","Player 2");

    if(playerchoice[current].getText().toString().equals(""))
    {
        if (savedTracker % 2 == 0)
        {
            playerchoice[current].setText(x);
            playerNameDisplay.setText(playerTwo + "'s Go!");
            savedTracker++;
        }
     }
 }

使用以下類。 這將幫助您擺脫所有令人困惑的代碼

public class PlayersData {

private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private static final String PLAYERS_DATA = "PLAYERS_DATA";
private static final String PLAYERS = "PLAYERS";

public PlayersData(Context context) {
    sharedPreferences = context.getSharedPreferences(PLAYERS_DATA, Context.MODE_PRIVATE);
    editor = sharedPreferences.edit();
}

public void savePlayers(Players players) {
    Gson gson = new Gson();
    String json = gson.toJson(players);
    editor.putString(PLAYERS, json);
    editor.commit();
}

public Players getPlayers() {
    Gson gson = new Gson();
    String json = sharedPreferences.getString(PLAYERS, null);
    return gson.fromJson(json, Players.class);
}

public class Players{

    private String one;
    private String two;

    public Players() {

    }

    public Players(String one, String two) {
        this.one = one;
        this.two = two;
    }

    public String getOne() {
        return one;
    }

    public void setOne(String one) {
        this.one = one;
    }

    public String getTwo() {
        return two;
    }

    public void setTwo(String two) {
        this.two = two;
    }
}

}

要獲取/保存數據簡單的初始化類並執行操作:

PlayersData playersData = new PlayersData(this); //this is the context

我忘記提及的一件事是我使用了 gson lib,因此將此行添加到您的應用程序級別 Gradle 文件並構建您的 Gradle:

implementation 'com.google.code.gson:gson:2.8.5'

代替

editor.commit();

editor.apply();

commit()將其首選項synchronously寫入持久存儲,這可能由於各種原因而失敗。 apply( ) 立即將其更改寫入內存中的 SharedPreferences,但會啟動對磁盤的asynchronous提交。

apply() 是完全安全的。 您可以放心使用它,因為您將獲得首選項的更新值。

暫無
暫無

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

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