0

I am attempting to save data from an edit text field within a dialog to an ArrayList. I followed the methods of an answer to a similar question (found here How to store edit text data from an Android dialog?) however my code isn't exactly saving data. From my understanding I should be able to do the following(note it isnt the entire class, only the method in question):

public class StartNewGameActivity extends AppCompatActivity {


    private ListView lv;
    ArrayList<String> game_players = new ArrayList<String>();


    public void addPlayerToGame(View view) {

        DialogFragment newFragment = new CreatePlayerDialogFragment(
                new CreatePlayerDialogFragment.MyMessageDialogListener(){
                    @Override
                    public void onClosed(String name) {
                    game_players.add(name);
                    }
                });
        newFragment.show(getSupportFragmentManager(), "players");
    }

While using the following class:

public class CreatePlayerDialogFragment extends DialogFragment {
    public interface MyMessageDialogListener {
        public void onClosed(String name);
    }



    public AlertDialog onCreateDialog(final MyMessageDialogListener listener) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final EditText input = new EditText(getActivity());
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        input.setLayoutParams(lp);
        builder.setView(input);
        builder.setMessage(R.string.player_name_dialog)
                .setPositiveButton(R.string.player_name_dialog_add, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // Add Player
                        {
                            if(listener != null) {
                                listener.onClosed(input.getText().toString());
                            }
                            dialog.cancel();
                        }
                    }
                })
                .setNegativeButton(R.string.player_name_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // User cancelled the dialog
                    }
                });
        // Create the AlertDialog object and return it
        return builder.create();
    }

}

However with my code I get an error "CreatePlayerDialogFrament() in CreatePlayerDialogFragment cannot be applied to CreatePlayerDialogFrament.MyMessageDialogListener"

Is it possibly because I am trying to do everything in my OnCreate() method? Im not quite sure what the issue is or how to go about solving it.

Community
  • 1
  • 1
Hunter Tipton
  • 313
  • 1
  • 4
  • 11

0 Answers0