从输入警告对话框将数据插入数据库

时间:2017-03-18 03:08:20

标签: android sqlite android-database

它是一个游戏,在结束时活动显示分数但在此之前,输入警告框显示在用户需要添加其名称的位置,该名称和分数应该转到数据库。分数正在存储但不是名称。如何从输入警告对话框中获取名称并将其设置在db.insertScore(Score,Name)。如果我想将添加警告对话框的输入值添加到extra.getString(“”);有吸气者和setter method.here是我的代码

Bundle extra = getIntent().getExtras();
    if (extra != null)
    {
        showInputDialog();
        final String Name=extra.getString("");
        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);

        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变量设为全局,不要使用以下行:

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

    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) {
                Name = editText.getText().toString();
            }
        });
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}

答案 1 :(得分:0)

试试这个

 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();
                //call a function which do in background and have a connection with database
             new setname(Name);
            }
        });
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
private void setname(final String name){
  @Override
protected String doInBackground(String... params){
 Data = new HashMap<String, String>();
 Data.put("name", name);
  try {
        JSONObject json = Connection.UrlConnection("http://url", Data);
        int succ = json.getInt("success");

        if (succ == 0) {
            s = "false";
        } else {
            s = "true";
        }


    } catch (Exception e) {
    }
    return s;
   }
  }

}

还要编写一个php文件,其中的代码插入到db