2011-07-30 73 views
0

是否有人能夠明白爲什麼我的名字的字符串沒有附加到值?組合字符串不附加到值

public void setNames() { 

    //*******************// 
    //***DATABASE INFO***// 
    //*******************// 
    DBAdapter db = new DBAdapter(this); 

    if (totalPlayerCount >= 1){ 


    //**********************// 
    //***SET PLAYER NAMES***// 
    //**********************// 
    AlertDialog.Builder alert = new AlertDialog.Builder(this); 

    alert.setTitle("Player " + nameLoop); 
    alert.setMessage("Name:"); 

    // Set an EditText view to get user input 
    final EditText input = new EditText(this); 
    alert.setView(input); 

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
     String newName = "name"+nameLoop; 
     // If i put "name1" here like so, the value "wow" stays with "name1" all the way to the database and does not end up null 
     name1 = "wow"; 
     newName = input.getText().toString(); <-- newName should be name1, name2, name3, name4 each time around. If i do a simple Toast, it displays name1, name2, etc. But when i insert those values into the database, they are all null. 
     setNames(); 
     } 
    }); 

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
     // Canceled. 
     } 
    }); 

    alert.show(); 

    nameLoop++; 

    } 
    if (totalPlayerCount == 0){ 
     db.open(); 
     db.insertPlayers(String.valueOf(name1), String.valueOf(name2), String.valueOf(name3), 
       String.valueOf(name4)); 
     db.close(); 

     AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this); 
     //myAlertDialog.setTitle("Saved"); 
     myAlertDialog.setMessage("Names saved"); 
     myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
       return; 
      } }); 
     myAlertDialog.show(); 
    } 
    totalPlayerCount--; 
    return; 
} 

這裏是同一剪輯剛剛打破的是我有

public void onClick(DialogInterface dialog, int whichButton) { 
     String newName = "name"+nameLoop; 
     // If i put "name1" here like so, the value "wow" stays with "name1" all the way to the database and does not end up null 
     name1 = "wow"; 
     newName = input.getText().toString(); <-- newName should be name1, name2, name3, name4 each time around. If i do a simple Toast, it displays name1, name2, etc. But when i insert those values into the database, they are all null. 
     setNames(); 
     } 
    }); 
+0

當你調用insertPlayers(...)調用String.valueOf(String)只是創建開銷,沒有有用的工作完成。 name1在哪裏申報? –

+0

name1-4在班級一開始都是公開的字符串..我想我可能會看到最新的錯誤,但不知道如何避開它。 我設置了newName =「name」+ nameLoop;它變成name1 但在此之後,newName = input.getText()。toString(); 這意味着newName不再是name1。 – Rob

+0

我想我需要創建一個數組或東西?名稱[]或類似的東西?不確定,但我需要名稱每次都從name1更改爲name2。 – Rob

回答

1

您不能動態創建變量,然後爲其分配一個值。 Java不能像JavaScript一樣工作。

你有一個switch語句來代替你的

String newName = "name"+nameLoop; 
    // If i put "name1" here like so, the value "wow" stays with "name1" all the way to the database and does not end up null 
    name1 = "wow"; 
    newName = input.getText().toString(); <-- newName should be name1, name2, name3, name4 each time around. If i do a simple Toast, it displays name1, name2, etc. But when i insert those values into the database, they are all null. 

部分。

String newName = input.getText().toString(); 
switch(nameLoop){ 
case 1: name1=newName;break; 
case 2: name2=newName;break; 
.... 
0

短版alert.show()的麻煩不會阻止這樣你面前正在執行數據庫插入」通過onClick重新獲得name1值分配。

Long版本,回過頭來學習一些Java的語法以及基於事件的編程是如何工作的。這是今天的這個second question。你渴望工作是一件好事,但我建議你在開始全速前進之前專注於一些基礎知識。有了一個很好的理解,你就可以用更少的壓力和更少的彎路來編碼父親和更快。 an ounce of prevention is worth a pound of cure