SharedPreferences问题

时间:2017-11-21 07:42:34

标签: android sharedpreferences

我是android开发的新手。我正在尝试制作一个简单的应用程序,以了解一些有用的方法。我得到了基本的应用程序,它只有几个TextView元素,通过按钮递增和递减。这是一个基本的记分员。

我遇到的问题是,只要程序关闭,屏幕就会旋转或关闭,数据就会丢失。

我正在尝试实现共享偏好以“保存”数据。有3个变量可以保存Home_Score,Visitor_Score和Inning_Number。

我被困了,因为数据没有正确恢复,我无法弄清楚发生了什么。

Inning_Number = 1
Home_Score = 2
Visitor_Score = 3

onResume显示数据:

Inning_Number = 0
Home_Score = 1
Visitor_Score = 3

看来第一个字段的值移动到第二个字段,第三个字段是正确的。

我尝试了很多方法,结果是一样的。我以为我混淆了变量,但如果我这样做,我就没有看到它。我坚持了,希望有人有答案。

提前致谢!

public class MainActivity extends AppCompatActivity {

// Create Variables to hold Field Data
Button btnIncrementHome;
Button btnDecrementHome;
Button btnIncrementVisitor;
Button btnDecrementVisitor;
Button btnIncrementInning;
Button btnDecrementInning;
Button btnReset;

TextView txthomeScore;
TextView txtvisitorScore;
TextView txtinningNumber;

//private SharedPreferences score;
private static final String Game_Score_FileName = "gameScore";
private String Home_Score = "txthomeScore";
private String Visitor_Score = "txtvisitorScore";
private String Inning_Number = "txtinningNumber";

SharedPreferences score;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Set default value in SharedPreferences file
    score = getSharedPreferences(Game_Score_FileName, Context.MODE_PRIVATE);
   //txthomeScore.setText(score.getString("Home_Score", "0"));
  // txtvisitorScore.setText(score.getString("Visitor_Score", "0"));
  // txtinningNumber.setText(score.getString("Inning_Number", "0"));

    // Get Values of the Form Fields
    btnIncrementHome = findViewById(R.id.buttonIncrementHome);
    btnDecrementHome = findViewById(R.id.buttonDecrementHome);
    btnIncrementVisitor = findViewById(R.id.buttonIncrementVisitor);
    btnDecrementVisitor = findViewById(R.id.buttonDecrementVisitor);
    btnIncrementInning = findViewById(R.id.buttonIncrementInning);
    btnDecrementInning = findViewById(R.id.buttonDecrementInning);
    btnReset = findViewById(R.id.buttonReset);
    txthomeScore = findViewById(R.id.homeScore);
    txtvisitorScore = findViewById(R.id.visitorScore);
    txtinningNumber = findViewById(R.id.inningNumber);


    btnIncrementHome.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String homeScoreValue = txthomeScore.getText().toString();
            int inthomeScoreValue = Integer.parseInt(homeScoreValue);
            inthomeScoreValue++;
            txthomeScore.setText(String.valueOf(inthomeScoreValue));
        }
    });

    btnDecrementHome.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String homeScoreValue = txthomeScore.getText().toString();
            int inthomeScoreValue = Integer.parseInt(homeScoreValue);
            inthomeScoreValue--;
            if (inthomeScoreValue < 0) {
                inthomeScoreValue=0;
            }
            txthomeScore.setText(String.valueOf(inthomeScoreValue));
        }
    });

    btnIncrementVisitor.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String visitorScoreValue = txtvisitorScore.getText().toString();
            int intvisitorScoreValue = Integer.parseInt(visitorScoreValue);
            intvisitorScoreValue++;
            txtvisitorScore.setText(String.valueOf(intvisitorScoreValue));
        }
    });

    btnDecrementVisitor.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String visitorScoreValue = txtvisitorScore.getText().toString();
            int intvisitorScoreValue = Integer.parseInt(visitorScoreValue);
            intvisitorScoreValue--;
            if (intvisitorScoreValue < 0) {
                intvisitorScoreValue=0;
            }
            txtvisitorScore.setText(String.valueOf(intvisitorScoreValue));
        }
    });

    btnIncrementInning.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String inningNumberValue = txtinningNumber.getText().toString();
            int intinningNumberValue = Integer.parseInt(inningNumberValue);
            intinningNumberValue++;
            txtinningNumber.setText(String.valueOf(intinningNumberValue));
        }
    });

    btnDecrementInning.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String inningNumberValue = txtinningNumber.getText().toString();
            int intinningNumberValue = Integer.parseInt(inningNumberValue);
            intinningNumberValue--;
            if (intinningNumberValue < 0) {
                intinningNumberValue=0;
            }
            txtinningNumber.setText(String.valueOf(intinningNumberValue));
        }
    });

    btnReset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            txthomeScore.setText(String.valueOf(0));
            txtvisitorScore.setText(String.valueOf(0));
            txtinningNumber.setText(String.valueOf(0));
        }
    });


}

@Override
protected void onPause(){
  super.onPause();

    String homeScore = txthomeScore.getText().toString();
    String visitorScore = txtvisitorScore.getText().toString();
    String inningNumber = txtinningNumber.getText().toString();

    score = getSharedPreferences(Game_Score_FileName, MODE_PRIVATE);
    SharedPreferences.Editor editor = score.edit();

  editor.putString("Inning_Number",inningNumber);
  editor.putString("Visitor_Score",visitorScore);
  editor.putString("Home_Score",homeScore);
  editor.apply();

}

@Override
protected void onResume(){
    super.onResume();

    score = getSharedPreferences(Game_Score_FileName, MODE_PRIVATE);

    String Home = score.getString("Home_Score","0");
    String Visitor = score.getString("Visitor_Score","0");
    String Inning = score.getString("Inning_Number","1");

    txthomeScore.setText(Home);
    txtvisitorScore.setText(Visitor);
    txthomeScore.setText(Inning);

}



}

2 个答案:

答案 0 :(得分:3)

您正在设置两次主场得分

txthomeScore.setText(Home);  <---- 
txtvisitorScore.setText(Visitor);
txthomeScore.setText(Inning); <----

替换为

txthomeScore.setText(Home);
txtvisitorScore.setText(Visitor);
txtinningNumber.setText(Inning);

由于您是Android新手,作为一般标准,使用camelCase作为局部变量

答案 1 :(得分:0)

替换您的活动代码

public class MainActivity extends AppCompatActivity {

// Create Variables to hold Field Data
Button btnIncrementHome;
Button btnDecrementHome;
Button btnIncrementVisitor;
Button btnDecrementVisitor;
Button btnIncrementInning;
Button btnDecrementInning;
Button btnReset;

TextView txthomeScore;
TextView txtvisitorScore;
TextView txtinningNumber;

//private SharedPreferences score;
private static final String Game_Score_FileName = "gameScore";
private String Home_Score = "txthomeScore";
private String Visitor_Score = "txtvisitorScore";
private String Inning_Number = "txtinningNumber";

SharedPreferences score;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Set default value in SharedPreferences file
    score = getSharedPreferences(Game_Score_FileName, Context.MODE_PRIVATE);

    // Get Values of the Form Fields
    btnIncrementHome = (Button) findViewById(R.id.btnIncrementHome);
    btnDecrementHome =(Button) findViewById(R.id.btnDecrementHome);
    btnIncrementVisitor =(Button) findViewById(R.id.btnIncrementVisitor);
    btnDecrementVisitor = (Button)findViewById(R.id.btnDecrementVisitor);
    btnIncrementInning =(Button) findViewById(R.id.btnIncrementInning);
    btnDecrementInning =(Button) findViewById(R.id.btnDecrementInning);
    btnReset = (Button)findViewById(R.id.btnReset);
    txthomeScore = (TextView) findViewById(R.id.txthomeScore);
    txtvisitorScore =(TextView) findViewById(R.id.txtvisitorScore);
    txtinningNumber =(TextView) findViewById(R.id.txtinningNumber);


    txthomeScore.setText(score.getString("Home_Score", "0"));
    txtvisitorScore.setText(score.getString("Visitor_Score", "0"));
    txtinningNumber.setText(score.getString("Inning_Number", "0"));


    btnIncrementHome.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String homeScoreValue = txthomeScore.getText().toString();
            int inthomeScoreValue = Integer.parseInt(homeScoreValue);
            inthomeScoreValue++;
            txthomeScore.setText(String.valueOf(inthomeScoreValue));
        }
    });

    btnDecrementHome.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String homeScoreValue = txthomeScore.getText().toString();
            int inthomeScoreValue = Integer.parseInt(homeScoreValue);
            inthomeScoreValue--;
            if (inthomeScoreValue < 0) {
                inthomeScoreValue=0;
            }
            txthomeScore.setText(String.valueOf(inthomeScoreValue));
        }
    });

    btnIncrementVisitor.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String visitorScoreValue = txtvisitorScore.getText().toString();
            int intvisitorScoreValue = Integer.parseInt(visitorScoreValue);
            intvisitorScoreValue++;
            txtvisitorScore.setText(String.valueOf(intvisitorScoreValue));
        }
    });

    btnDecrementVisitor.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String visitorScoreValue = txtvisitorScore.getText().toString();
            int intvisitorScoreValue = Integer.parseInt(visitorScoreValue);
            intvisitorScoreValue--;
            if (intvisitorScoreValue < 0) {
                intvisitorScoreValue=0;
            }
            txtvisitorScore.setText(String.valueOf(intvisitorScoreValue));
        }
    });

    btnIncrementInning.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String inningNumberValue = txtinningNumber.getText().toString();
            int intinningNumberValue = Integer.parseInt(inningNumberValue);
            intinningNumberValue++;
            txtinningNumber.setText(String.valueOf(intinningNumberValue));
        }
    });

    btnDecrementInning.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String inningNumberValue = txtinningNumber.getText().toString();
            int intinningNumberValue = Integer.parseInt(inningNumberValue);
            intinningNumberValue--;
            if (intinningNumberValue < 0) {
                intinningNumberValue=0;
            }
            txtinningNumber.setText(String.valueOf(intinningNumberValue));
        }
    });

    btnReset.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            txthomeScore.setText(String.valueOf(0));
            txtvisitorScore.setText(String.valueOf(0));
            txtinningNumber.setText(String.valueOf(0));
        }
    });


}

@Override
protected void onPause(){
    super.onPause();

    String homeScore = txthomeScore.getText().toString();
    String visitorScore = txtvisitorScore.getText().toString();
    String inningNumber = txtinningNumber.getText().toString();

    score = getSharedPreferences(Game_Score_FileName, MODE_PRIVATE);
    SharedPreferences.Editor editor = score.edit();

    editor.putString("Inning_Number",inningNumber);
    editor.putString("Visitor_Score",visitorScore);
    editor.putString("Home_Score",homeScore);
    editor.apply();

}

@Override
protected void onResume(){
    super.onResume();

    score = getSharedPreferences(Game_Score_FileName, MODE_PRIVATE);

    String Home = score.getString("Home_Score","0");
    String Visitor = score.getString("Visitor_Score","0");
    String Inning = score.getString("Inning_Number","1");

    txthomeScore.setText(Home);
    txtvisitorScore.setText(Visitor);
    txtinningNumber.setText(Inning);

}

}