我如何将信息从 activity 传递到自定义警报对话框中的编辑文本

How do i pass information from an activity to an edittext in a custom alertdialog

我制作了一个自定义警报对话框,其中包含 2 个 Edittext。我想在单击某些内容时将信息从现有数据库传递给它。

这是自定义警报对话框的 class

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

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

        builder.setView(view).setTitle("Add Job")

                .setPositiveButton("add", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        if(isDouble(wageValue.getText().toString())){
                            String nameOjob = jobName.getText().toString();
                            Double valueOwage = Double.parseDouble(wageValue.getText().toString());
                            listener.applyTexts(nameOjob, valueOwage);
                            builder.create();

                        }else{
                            wageValue.setError("Wrong Format Error");
                        }

                    }
                })
                .setNeutralButton("cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });


        jobName = view.findViewById(R.id.job_name_ETD);
        wageValue = view.findViewById(R.id.wage_ETD);

        //I want to pass information to these

        //jobName.setText();
        //wageValue.setText();

        return builder.create();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            listener =(ExampleDialogueListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + "must implement Example Dialogue Listener");
        }
    }

    public interface ExampleDialogueListener{
        void applyTexts(String jobname, Double wage);
    }

    boolean isDouble(String str) {
        try {
            Double.parseDouble(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

}

我想通过参数传递信息给edittext,这样我创建alertdialog的时候edit text上就有信息了

您似乎在扩展 DialogFragment。

您可以像传递常规 Fragment 一样传递数据。

Bundle args = new Bundle();
args.putString("arg_key","something");
f.setArguments(args);

其中 f 是您的 DialogFragment 实例。

在您的 DialogFragment 中,您可以调用 getArguments() 来获取您的包。