2017-08-04 9 views
-2

Iは、入力表か裏にユーザーを要求するアプリを持っている:新しいアクティビティからalertDialog情報にアクセスするには?

   AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       builder.setTitle("Heads or Tails?") 
         .setSingleChoiceItems(coinOptions, 0, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           dialog.dismiss(); 
           //start coin toss 
           startPlay(which, coins); 
          } 
         }); 
       AlertDialog ad = builder.create(); 
       ad.show(); 

は、私はちょうどそれが新しいページに渡している正確に何か分かりませんか?それは整数ですか、0か1か他の何かですか?次のように私はそれを渡しています:

private void startPlay(int coinOption, int coins) 
    { 
     //start cointoss 
     Intent playIntent = new Intent(this, CoinToss.class); 
     playIntent.putExtra("bet", coinOption); 
     playIntent.putExtra("coins", coins); 
     this.startActivity(playIntent); 
    } 

、それが供給:

Bundle extras = getIntent().getExtras(); 
     int totalCoins = extras.getInt("coins", -1); 
     int bet = extras.getInt("bet", -1); 

私は頭や尾が変数「ベット」の下でなければなりません私に言ってほしいな情報を。

+1

どうしたのですか? – lelloman

+1

これはcoinOptionsの位置、つまり0または1を返します –

+0

申し訳ありませんが、私は明確ではありませんでした。私はこれに新しいですし、私は何をやっているのか分かりません。ヘッドをクリックすると、どの値が「賭け」に割り当てられますか? 1または0、または何か他の? :) – Emma

答えて

1

あなたの実装では、あなたの警報dailogは、その後、あなたのスタートプレイはこの

private void startPlay(int coinOptionSelected, int coins) 
    { 
     //start cointoss 
     Intent playIntent = new Intent(this, CoinToss.class); 

      String selectedValue =null; 
      if(coinOptionSelected != -1)  // if -1 means no option seletced do your rest handlings with else block 
      { 
        selectedValue = coinOptions[coinOptionSelected]; // if coinOptionSelected =1 then it will give Heads and if coinOptionSelected = 1 then it will give Tails 
      } 


     playIntent.putExtra("bet", selectedValue); 
     playIntent.putExtra("coins", coins); // i dont know what this value you have 
     this.startActivity(playIntent); 
    } 

最後に、あなたの受信がすべきようにする必要があり、この

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       builder.setTitle("Heads or Tails?") 
         .setSingleChoiceItems(coinOptions, -1, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           dialog.dismiss(); 
           //start coin toss 

           int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition(); 
           // Do something useful withe the position of the selected option 
           startPlay(selectedPosition , coins); 
          } 
         }); 
       AlertDialog ad = builder.create(); 
       ad.show(); 

ようにする必要があり、この

String [] coinOptions = {"Heads", "Tails"}; // adding your coin options 

ようにする必要がありますこのようになる

 Bundle extras = getIntent().getExtras(); 
     String selectedValue = extras.getString("bet"); // it will return Heads or Tails 
     int bet = extras.getInt("coins", -1);  // i dont know what this value you have 
+0

@Emmaあなたは私の答えを試しました –

+1

あなたは信じられないほどです最後に、if(coinOptions!= -1)文でエラーが発生します。 'Operator'!= 'Java.Lang.Charsequence'にも適用できません。また、 '互換性のない型'もあります。 – Emma

+0

何かを文字列に変換する必要がありますか? – Emma

関連する問題