如何从对话框类回调到Fragment

问题描述 投票:0回答:1

我试图在带有日期选择器的对话框关闭后更新按钮上的文本。 该对话框在片段类中实例化,按钮位于该类中 同班。这是一些代码:

片段类:

select_dob_btn = view.findViewById(R.id.profile_dob_button);
    datePickerDialog = new DatePickerDialog();
    datePickerDialog.setButtonText("Select Date Of Birth");
    select_dob_btn.setText(datePickerDialog.getButtonText());

整个对话框类:

public class DatePickerDialog extends DialogFragment {

public long dateResult = 0;
String buttonText = "";

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //int listingID = getArguments().getInt("listingID");


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

    // Get the layout inflater
    LayoutInflater inflater = this.getLayoutInflater();

    View custom_layout = inflater.inflate(R.layout.dialog_date_picker,null);

    DatePicker datePicker = custom_layout.findViewById(R.id.dialog_date_picker);
    if(dateResult != 0){
        datePicker.init(Globals.getDateYearFromTimeStamp(dateResult), Globals.getDateMonthFromTimeStamp(dateResult),Globals.getDateDayFromTimeStamp(dateResult), null);

    }




    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(custom_layout)
            .setPositiveButton(R.string.submit, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dateResult = Globals.getTimeStampFromDateDDMMYYYY(datePicker.getDayOfMonth()+"-"+(datePicker.getMonth()+1)+"-"+datePicker.getYear());
                    setButtonText(String.valueOf(Globals.getDateFromTimeStamp(dateResult)));
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dateResult = 0;
                    setButtonText(String.valueOf(R.string.register_select_dob));
                    dialogInterface.cancel();

                }
            });


    return builder.create();
}
public DatePickerDialog newInstance() {
    DatePickerDialog f = this;

    // Supply num input as an argument.
    Bundle args = new Bundle();
   // args.putInt("listingID", 0);

    f.setArguments(args);

    return f;
}
public void ShowDialog(FragmentManager fragmentManager) {
    DialogFragment newFragment = newInstance();

    newFragment.show(fragmentManager, String.valueOf(0));
}

public void setButtonText(String text){
    buttonText = text;
}
public String getButtonText(){
    return buttonText;
}

}

现在回到 Fragment 类,我想触发此方法来更新“选择出生日期”按钮:

    void RefreshSetBirthdayButton(){

    if(datePickerDialog.dateResult == 0){
        select_dob_btn.setText(R.string.register_select_dob);
    }else{
        datePickerDialog.setButtonText(String.valueOf(Globals.getDateFromTimeStamp(datePickerDialog.dateResult)));
        select_dob_btn.setText(datePickerDialog.getButtonText());
    }

}

我尝试在初始化对话框类后添加 onDateChangeLIstener 但是, 我收到 NullpointerException。

任何想法都会有帮助谢谢!

java android datepicker dialog
1个回答
0
投票

您必须遵循 Android 架构背后的

MVC
MVVM
模式。在这种情况下,UI 上的所有更改都基于模型的更改。所以我必须更新模型状态,并且 UI 应收到有关模型更改的通知。

执行此操作的正确方法是使用

ViewModel
类,该类在主机片段和对话框片段之间共享单个实例。您可以使用 Sunflower android app 进行参考,它有点过时了(例如,它使用
LiveData
而不是
Kotlin Flows
),但仍然显示了一些很好的架构决策。

另一种方法是通过回调实现接口:

interface IDialogCallbacks {
  fun onClose()
}

制作一个主机片段来实现此接口:

class MainFragment: Fragment, IDialogCallbacks { /* the rest of code */}

在对话框片段(或任何其他子片段)调用父片段时,检查它是否实现此接口,如果是则调用它:

    if(getParentFragment() is IDialogCallbacks) {
        (getParentFragment() as IDialogCallbacks).onClose()
    } 
© www.soinside.com 2019 - 2024. All rights reserved.