1

Its a game in which at the end activity the score is displayed But before that the input alert box is displayed where user need to add their name,and that name and score should go to the database. score is getting stored but not the name. how to get name from input alert dialog box and set it on db.insertScore(Score,Name). Even declare Name as global it still not working here is my code

  Bundle extra = getIntent().getExtras();
    if (extra != null)
    {
        showInputDialog();
        final int Score = extra.getInt("SCORE");
        final int totalQuestion = extra.getInt("TOTAL");
        int correctAnswer = extra.getInt("CORRECT");
        txtTotalScore.setText(String.format("SCORE : %d", Score));
        txtTotalQuestion.setText(String.format("PASSED : %d/%d", correctAnswer, totalQuestion));

        progressBarResult.setMax(totalQuestion);
        progressBarResult.setProgress(correctAnswer);

        //save score
       db.insertScore(Score,Name);
    }
}


protected void showInputDialog() {

    // get prompts.xml view
    LayoutInflater layoutInflater = LayoutInflater.from(Done.this);
    View promptView = layoutInflater.inflate(R.layout.dialog, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Done.this);
    alertDialogBuilder.setView(promptView);

    final EditText editText = (EditText) promptView.findViewById(R.id.edittext);
    // setup a dialog window
    alertDialogBuilder.setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            String Name = editText.getText().toString();
        }
    });
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
}

2 Answers2

0

The reason it's not being added is because you define the Name variable in the dialog box, which limits the scope of your variable to that condition. If I were you, I would create a class variable, called name, then create a getter and setter for the variable. Then inside your dialog box, you could call setName(name). Then when you store the data in your database, you can call db.insertScore(Score,getName());. Also, FWIW, variables should be lowercase. Uppercase is typically reserved for class names.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0

Create 1 more function to store the score and edited name into database. Follow the code below.

    Bundle extra = getIntent().getExtras();
    if (extra != null)
    {
        final int Score = extra.getInt("SCORE");
        final int totalQuestion = extra.getInt("TOTAL");
        int correctAnswer = extra.getInt("CORRECT");
        txtTotalScore.setText(String.format("SCORE : %d", Score));
        txtTotalQuestion.setText(String.format("PASSED : %d/%d", correctAnswer, totalQuestion));

        progressBarResult.setMax(totalQuestion);
        progressBarResult.setProgress(correctAnswer);



        //show dialog
        showInputDialog(Score);
    }


    protected void showInputDialog(final int score) {

        // get prompts.xml view
        LayoutInflater layoutInflater = LayoutInflater.from(Done.this);
        View promptView = layoutInflater.inflate(R.layout.dialog, null);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Done.this);
        alertDialogBuilder.setView(promptView);

        final EditText editText = (EditText) promptView.findViewById(R.id.edittext);
        // setup a dialog window
        alertDialogBuilder.setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        String Name = editText.getText().toString();

                        storeData(Name, score);
                    }
                });
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();
    }


    protected void storeData(String name, final int score){
        db.insertScore(score,name);
    }
Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
  • It has stop working. when the page reaches at this activity it stopped –  Mar 17 '17 at 17:42