Pergunta

I'm implementing a guess number apps.

the user will try to guess the random number picked by the computer

when the user answer with correct answer, then the user will get score 1 and play again which means the activity will be refresh

if the user answer the next number with correct answer, then the score will increment by one until the user got the wrong answer, then the score will stop there

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

that is my code

I always get 1 as the score by using those code

is there a way to refresh activity with storing last activity result?

thanks

Foi útil?

Solução

For this case I would simply use SharedPrefs to store your data.

Try this - here is the relevant part of your code with my addition:

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

That should take care of that... Take note that the SharedPrefs data remains after application is closed, so if at any time you need to reset the data stored just delete the entry in it using:

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

Hope this helped!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top