-1

I'm trying to create a custom dialog where it will show a toast when the user click on the positive button, but I got an error on the Toast line. What is the problem and how to resolve it? I think it has something to do with the context. The error just says: Cannot resolve method 'makeText(com.example.studentsacademicmanagementappsama.SubjectListDialog, java.lang.String, int)'

SubjectListDialog.java

public class SubjectListDialog extends AppCompatDialogFragment {
    private EditText et_subjectCode, et_subjectName, et_creditHour;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

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

        builder.setView(view)
                .setTitle("Add Subject")
                .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .setPositiveButton("confirm", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        try {
                            SubjectListModel subjectListModel = new SubjectListModel(et_subjectCode.
                                    getText().toString(),et_subjectName.getText().toString(),
                                    Integer.parseInt(et_creditHour.getText().toString()));

                            Toast.makeText(SubjectListDialog.this, subjectListModel.toString(),
                                    Toast.LENGTH_SHORT).show();
                        }catch (Exception e){

                        }
                    }
                });
        et_subjectCode = view.findViewById(R.id.et_subjectCode);
        et_subjectName = view.findViewById(R.id.et_subjectName);
        et_creditHour = view.findViewById(R.id.et_creditHour);
        return builder.create();
    }
}
Irfan
  • 77
  • 1
  • 9
  • `but I got an error on the Toast line` well, what's the error ? you probably can't use `SubjectListDialog.this` – a_local_nobody Dec 03 '21 at 09:33
  • @a_local_nobody the error just says Cannot resolve method 'makeText(com.example.studentsacademicmanagementappsama.SubjectListDialog, java.lang.String, int)' If I cant use SubjectListDialog.this then what should I do? – Irfan Dec 03 '21 at 09:42

2 Answers2

3

Toast.makeText(view.getContext(), subjectListModel.toString(), Toast.LENGTH_SHORT).show();

gaurav kumar
  • 368
  • 2
  • 8
1

The value to pass is the context.

You are passing the wrong value, if you pass 'SubjectListDialog.this' you are passing the SubjectListDialog class itself, not the context.

Toast.makeText(requireContext(), subjectListModel.toString(),Toast.LENGTH_SHORT).show();