2014-05-19 66 views
1

我遇到了只調用一次onCreateDialog的問題。這對我來說是個問題,因爲我已經在該方法中引用了xml佈局,並且每當我關閉對話框並在第二次調用它時,這些引用都消失了。下面的代碼:刪除Alertdialog,使newAlertDialog()將調用onCreateDialog

 if (position == 0){ 

     PlayerNameDialog playerNameDialog = PlayerNameDialog.newInstance(); 
     playerNameDialog.show(getFragmentManager(), TeamActivity.PLAYER_DIALOG); 

    } 
    else { 
     String playerName = teamPlayers.get(position).toString(); 
     Player selectedPlayer = team.FindPlayer(playerName); 
     PlayerNameDialog playerNameDialog = PlayerNameDialog.newInstance(); 



     playerNameDialog.changePlayerName(selectedPlayer.playerName); 
     playerNameDialog.changePlayerRating(selectedPlayer.playerRating); 
     playerNameDialog.show(getFragmentManager(), TeamActivity.PLAYER_DIALOG); 

    } 

和我的警告對話框

public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreateDialog(savedInstanceState); 

    LayoutInflater inflater = getActivity().getLayoutInflater(); 
    view = inflater.inflate(R.layout.player_name_dialog, null); 

    playerRatingSeekBar = (SeekBar) view.findViewById(R.id.player_rating_sbar); 
    playerRatingSeekBar.setOnSeekBarChangeListener(this); 
    playerRatingTxtView = (TextView) view.findViewById(R.id.player_rating); 
    playerRatingTxtView.setText(getString(R.string.player_rating)+" "+playerRatingSeekBar.getProgress()); 
    playerNameEditText = (EditText) view.findViewById(R.id.player_name); 

    builder = new AlertDialog.Builder(getActivity()); 

    builder.setView(view); 



    builder.setMessage("New Player"); 
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){ 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 

      mListner.onPNDialogPositiveClick(PlayerNameDialog.this, view); 

     } 

    }); 
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      mListner.onPNDialogNegativeClick(PlayerNameDialog.this, view); 

     } 
    }); 

    return builder.create(); 

} 

我第二次創造一個新的對話實例(下else語句),該onCreateDialog方法不再調用。結果所有的edittext引用都沒有了。我嘗試使用dimiss()和removefragment方法,但沒有工作

無論如何解決這個問題?

在此先感謝。

+0

耶創建另一個對話框類應該像你,而不是試圖手動調用'onCreateDialog',你永遠不應該對你有所幫助做 – tyczj

回答

1
try this: 
private static AlertDialog dialog; 
public static void showDialog(Context context, String message) { 
    if (context != null) { 
     TextView textMsg; 
     TextView textOk; 
     LinearLayout textOKLayout; 

     AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 
     layoutInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View dialogView = layoutInflator.inflate(R.layout.alert_dialog, null); 
     alertDialog.setView(dialogView); 
     textMsg = (TextView) dialogView.findViewById(R.id.text); 
     textOk = (TextView) dialogView.findViewById(R.id.textOk); 
     textOKLayout = (LinearLayout) dialogView.findViewById(R.id.textOKLayout); 
     textMsg.setText(message); 
     textOKLayout.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       dialog.dismiss(); 
      } 
     }); 

     dialog = alertDialog.create(); 
     dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0)); 
     dialog.show(); 
    } else { 
     return; 
    } 

} 
+0

謝謝,但我使用的,而不是對話框類dialogFragment ... –

+0

檢查更新答案..通過你的活動作爲背景 –