如何将值添加到从输入警告对话框中获取的数据库中

时间:2017-03-17 17:18:15

标签: android database android-studio android-database

它是一个游戏,在结束时活动显示分数但在此之前,输入警告框显示在用户需要添加其名称的位置,该名称和分数应该转到数据库。分数正在存储但不是名称。如何从输入警告对话框中获取名称并将其设置在db.insertScore(Score,Name)上。即使将Name声明为全局,它仍然无效 这是我的代码

  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 个答案:

答案 0 :(得分:0)

未添加它的原因是因为您在对话框中定义了Name变量,这会将变量的范围限制为该条件。如果我是你,我会创建一个名为name的类变量,然后为变量创建一个getter和setter。然后在对话框中,您可以调用setName(name)。然后,当您将数据存储在数据库中时,可以调用db.insertScore(Score,getName());。另外,FWIW,变量应该是小写的。大写通常保留给类名。

答案 1 :(得分:0)

再创建一个函数,将分数和编辑后的名称存储到数据库中。请遵循以下代码。

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