Android 从“输入警报”对话框将数据插入数据库
这是一个在活动结束时显示分数的游戏,但在此之前,将显示输入警报框,用户需要在其中添加自己的姓名,并且该姓名和分数应进入数据库。正在存储分数,但未存储名称。如何从输入警报对话框中获取名称并将其设置在db.insertScore(Score,name)上。如何将警报对话框的输入值添加到extra.getString(“”);还有getter和setter方法。这是我的代码Android 从“输入警报”对话框将数据插入数据库,android,sqlite,android-database,Android,Sqlite,Android Database,这是一个在活动结束时显示分数的游戏,但在此之前,将显示输入警报框,用户需要在其中添加自己的姓名,并且该姓名和分数应进入数据库。正在存储分数,但未存储名称。如何从输入警报对话框中获取名称并将其设置在db.insertScore(Score,name)上。如何将警报对话框的输入值添加到extra.getString(“”);还有getter和setter方法。这是我的代码 Bundle extra = getIntent().getExtras(); if (extra != null)
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();
}
将名称变量设为全局变量,不要使用以下行:
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();
}
试试这个
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;
}
}
}
受保护的void showInputDialog(){
//获取prompts.xml视图
LayoutInflater LayoutInflater=LayoutInflater.from(Done.this);
View promptView=LayoutFlater.充气(R.layout.dialog,null);
AlertDialog.Builder alertDialogBuilder=新建AlertDialog.Builder(完成.this);
alertDialogBuilder.setView(promptView);
final EditText EditText=(EditText)promptView.findViewById(R.id.EditText);
//设置对话框窗口
alertDialogBuilder.setCancelable(false)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
字符串名称=editText.getText().toString();
//调用一个在后台执行并与数据库连接的函数
新设置名称(名称);
}
});
AlertDialog alert=alertDialogBuilder.create();
alert.show();
}
私有void集合名(最终字符串名){
@凌驾
受保护的字符串doInBackground(字符串…参数){
Data=newhashmap();
数据输入(“名称”,名称);
试一试{
JSONObject json=Connection.UrlConnection(“http://url“,数据);
intsucc=json.getInt(“成功”);
如果(成功==0){
s=“假”;
}否则{
s=“真”;
}
}捕获(例外e){
}
返回s;
}
}
}
还要编写一个php文件,将代码插入db是否有任何简单的编码?是否容易混淆?您想说什么?