时间:2011-07-30 02:17:42

标签: java android database string null

是否有人能够理解为什么我的带有组成名称的字符串没有附加到某个值?

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

2 个答案:

答案 0 :(得分: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;
....

答案 1 :(得分:0)

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

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