2015-12-29 24 views
-2

我遇到問題後,無處不在搜索到如何將警報對話元素或彈出元素存儲在變量中。在Intent中傳遞AlertDialog元素(Android)

例如, 在我的彈出窗口中,我通過php從json獲得了像Principal,Incharge和Teacher這樣的值。

不是我想要將Intent中的Selected Item移動到Intent中傳遞(Principal)。

我正在努力爲此。請幫忙 !

AlertDialog.Builder builderSingle = new AlertDialog.Builder(context); 
     builderSingle.setIcon(R.drawable.logo); 
     builderSingle.setTitle("Select One Recipent:-"); 

     final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, 
       android.R.layout.select_dialog_singlechoice); 
     for (i = 0; i < myData.length; i++) { 
      arrayAdapter.add(myData[i]); 
      selecteditem = myData[i]; /// Should be the selected item but its not i know 
     } 

     builderSingle.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
      } 
     }); 

     builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       String strName = arrayAdapter.getItem(which); 
       AlertDialog.Builder builderInner = new AlertDialog.Builder(context); 
       builderInner.setMessage(strName); 
       Intent intent = new Intent(context,send_communication_to.class); 
       intent.putExtra("Authoritytype", selecteditem); 
       context.startActivity(intent); 
+0

解釋你的問題。 – Jas

+0

問題是,我想通過選擇的項目的彈出窗口例如**老師**到下一頁。我們可以實現這個 –

+0

爲什麼你不能使用'意圖'? – Jas

回答

0

代替intent.putExtra("Authoritytype", selecteditem); 使用存儲在selecteditemintent.putExtra("Authoritytype", strName);

值始終是最後一項myDataselecteditem值被設置for循環內

for (i = 0; i < myData.length; i++) { 
      arrayAdapter.add(myData[i]); 
      selecteditem = myData[i]; /// Should be the selected item but its not i know 
     } 

OR 更換String strName = arrayAdapter.getItem(which);

selecteditem = arrayAdapter.getItem(which); 
+0

感謝噸!我剛剛更換了 intent.putExtra(「Authoritytype」,** selecteditem **); ** TO ** intent.putExtra(「Authoritytype」,** arrayAdapter.getItem(which)**); 它對我很有用 –

+0

@TusharThurawa:我很高興我能幫助你,請將此標記爲正確答案 –

+0

yaa當然,但我沒有那麼多聲望投票:-( –