gpt4 book ai didi

java - 组合字符串未附加到值

转载 作者:行者123 更新时间:2023-12-01 15:47:19 25 4
gpt4 key购买 nike

有人能明白为什么我的带有虚构名称的字符串没有将其自身附加到值上吗?

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

最佳答案

您无法动态创建变量然后为其赋值。 Java 的工作方式与 JavaScript 不同。

你必须更换你的

  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.

带有 switch 语句的部分。

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

关于java - 组合字符串未附加到值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6880678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com