存储最后一个活动结果Android的刷新活动

时间:2014-05-01 07:06:26

标签: android button page-refresh

我正在实施猜测号码应用。

用户将尝试猜测计算机选择的随机数

当用户回答正确答案时,用户将获得分数1并再次播放,这意味着活动将刷新

如果用户用正确的答案回答下一个号码,则分数将增加1,直到用户得到错误答案,然后分数将停在那里

int userScore = 0;
public void userAnswer(){

    score = new ArrayList<Integer>();
    //user input in edittext
    userInput = new EditText(this);
    userInput.setInputType(InputType.TYPE_CLASS_NUMBER);
    //set alert dialog
    AlertDialog.Builder answerAlert = new AlertDialog.Builder(this);
    final AlertDialog.Builder resultAlert = new AlertDialog.Builder(this);
    answerAlert.setTitle("Your Answer");
    answerAlert.setMessage("What is the number?");
    answerAlert.setIcon(R.drawable.answer);
    answerAlert.setView(userInput);

    userInput.setOnFocusChangeListener(new OnFocusChangeListener() {

        public void onFocusChange(View v, boolean hasFocus) {
            userInput.setHint("Enter your answer here");                        
        }
    });

    answerAlert.setNegativeButton("Answer", new OnClickListener() {
        @Override
        //check the user answer with computer number
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();

            //set focus on enter user name


            if(Integer.parseInt(userInput.getText().toString()) == compNumber){
                resultAlert.setTitle("Result");
                resultAlert.setIcon(R.drawable.correct);
                resultAlert.setMessage("Correct answer. The number is " + compNumber);
                userScore++;
                System.out.println("========================================user score: "+userScore);

                //CHANGE THE ACTIVITY
                resultAlert.setPositiveButton("Play Again", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = getIntent();
                        startActivity(intent);
                    }
                });
                resultAlert.show();

            }else{
                resultAlert.setTitle("Result");
                resultAlert.setIcon(R.drawable.wrong);
                resultAlert.setMessage("Wrong answer. The number is " + compNumber);

                resultAlert.setNegativeButton("Back To Main Menu", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(GamePlay.this, MainActivity.class);
                        startActivity(intent);
                    }
                });
                resultAlert.show();
            }


        }
    });
    answerAlert.show();
}

这是我的代码

我总是通过使用这些代码获得1分

有没有办法刷新存储上次活动结果的活动?

感谢

1 个答案:

答案 0 :(得分:1)

对于这种情况,我只需使用SharedPrefs来存储您的数据。

试试这个 - 这是我的补充代码的相关部分:

  if(Integer.parseInt(userInput.getText().toString()) == compNumber){
                resultAlert.setTitle("Result");
                resultAlert.setIcon(R.drawable.correct);
                resultAlert.setMessage("Correct answer. The number is " + compNumber);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);

int userScore = pref.getInt("score", 0); // getting last score from the SharedPrefs, if there is no previous score it sets it to 0
                userScore++;
Editor editor = pref.edit();
editor.putInt("score", userScore); // Storing the new user score
editor.commit(); //saving the changes

                System.out.println("========================================user score: "+userScore);

                //CHANGE THE ACTIVITY
                resultAlert.setPositiveButton("Play Again", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = getIntent();
                        startActivity(intent);
                    }
                });
                resultAlert.show();

那应该照顾好...... 请注意,在应用程序关闭后,SharedPrefs数据仍然存在,因此如果您需要重置存储的数据,只需使用以下命令删除其中的条目:

editor.remove("score"); // will delete the score entry
editor.commit(); // commit changes

希望这有帮助!