2017-03-18 100 views
0

它是一個遊戲,在最後的活動中顯示分數但在此之前,輸入警報框顯示在用戶需要添加其名稱的位置,並且該名稱和分數應該到達數據庫。分數正在被儲存,但不是名稱。如何從輸入警報對話框中獲取名稱並將其設置爲db.insertScore(Score,Name)。我是如何將警報對話框的輸入值添加到extra.getString(「」);有吸氣&二傳手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(); 
} 
+0

你想說什麼? –

回答

0

使名稱變量全球和不使用以下行:

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

查看我的代碼。你有沒有嘗試確切的代碼? –

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

} 

還寫代碼插入到db的一個php文件

+1

是不是有簡單的編碼? –

+0

是否令人困惑? –