【问题标题】:Combined String not attaching to value组合字符串不附加到值
【发布时间】:2011-07-30 02:17:42
【问题描述】:

有没有人能看出为什么我的带有虚构名称的字符串没有附加到一个值上?

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

【问题讨论】:

  • 当您调用 insertPlayers(...) 时,调用 String.valueOf(String) 只会产生开销,并且不会完成任何有用的工作。 name1 在哪里声明?
  • name1-4 都是在课程开始时声明的公共字符串。我想我可能会看到有什么问题,但不知道如何解决它。我设置 newName = "name"+nameLoop;它变成了 name1 但在那之后, newName = input.getText().toString();这意味着 newName 不再是 name1。
  • 我想我需要创建一个数组什么的??名称[] 或类似的东西?不确定,但我每次都需要将名称从 name1 更改为 name2。
  • 我不知道您在哪里设置 name2 name3 等的值。如果您想将用户输入的每个名称插入到数据库中。只需声明一个字符串列表并添加名称到列表,然后迭代它并插入数据库...

标签: java android database string null


【解决方案1】:

您不能动态创建变量然后为其赋值。 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;
....

【讨论】:

    【解决方案2】:

    短版本 alert.show() 不会阻塞,因此您在通过 onClick 获取 name1 值分配之前执行数据库插入。

    长版,退后几步,学习一些 Java 的语法以及基于事件的编程是如何工作的。这就是今天这样的second question。你渴望工作是一件好事,但我建议你在开始全力以赴之前专注于一些基础知识。有了很好的理解,您将能够以更少的压力和更少的弯路更快地编写父亲和更快的代码。 an ounce of prevention is worth a pound of cure

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 2020-11-18
      • 2023-01-19
      • 2018-03-12
      • 2013-01-09
      • 2011-01-18
      相关资源
      最近更新 更多