2011-07-30 3 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はすべてクラスの初めに公開ストリングと宣言されています。私は何が間違っているのか分かりませんが、それを回避する方法はわかります。 i newName = "name" + nameLoopを設定します。それは名前1になります。 しかし、その直後に、newName = input.getText()。toString(); つまり、newNameは名前1ではありません。 – 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()とのトラブルを抱えていていることが破られます名前1の値の割り当てをonClickで取得し直しています。

ロングバージョンでは、いくつかのステップを踏んで、Javaの構文とイベントベースのプログラミングの仕組みについて学びます。これは今日このようなsecond questionです。あなたが仕事に熱意を持っていることはすばらしいことですが、フルスチームを上手く実行する前に、いくつかの基本事項に集中することをおすすめします。理解が深ければ、ストレスが少なく、迂回回数が少なく、父親を素早くコーディングすることができます。 an ounce of prevention is worth a pound of cure

関連する問題