Reputation: 2293
I run into the problem where onCreateDialog is called only once. This is a problem for me because i have referred to xml layouts in that method and whenever i dismiss the dialog and call it the second time, these references are gone. code below:
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);
}
and for my alert 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();
}
the second time i create a new dialog instance (under else statement), the onCreateDialog method is no longer called. As a result all the edittext references are gone. I tried using the dimiss() and removefragment method, but did not work
Anyway to solve this problem?
Thanks in advance.
Upvotes: 1
Views: 66
Reputation: 363
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;
}
}
Upvotes: 1