将数据从DialogFragment发送到Fragment

时间:2014-11-04 11:49:38

标签: android android-fragments

我有一个包含EditText的片段,当我点击EditText时,会出现一个DatePicker对话框来选择日期。
FragmentFile

public class FirstFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first, container, false);
        EditText editText = (EditText) view.findViewById(R.id.visitDateFiled);

        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogFragment picker = new DatePickerFragment();
                picker.show(getFragmentManager(), "Date Picker");
            }
        });
        return view;
    }

DialogFragment

public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        String formattedDate = sdf.format(c.getTime());

    }
}

问题我不知道如何将数据(选定日期)从DialogFragment传递到Fragment?
 我读了一些话题,但它没有让我感到困惑(很抱歉再次提出这个问题)。

Topic 1 Topic 2

4 个答案:

答案 0 :(得分:3)

更新了代码


FragmentFile

public class FirstFragment extends Fragment implements DatePickerDialogFragmentEvents{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_first, container, false);
        EditText editText = (EditText) view.findViewById(R.id.visitDateFiled);

        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogFragment picker = new DatePickerFragment();
                picker.setDatePickerDialogFragmentEvents(FirstFragment.this); //Changed
                picker.show(getFragmentManager(), "Date Picker");
            }
        });
        return view;
    }

    //This is where you will get selected date value
    @Override
    public void onDateSelected(String date) {
        String selectedDate = date;
    } 
 }

DialogFragment

public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    //Interface created for communicating this dialog fragment events to called fragment
    public interface DatePickerDialogFragmentEvents{
       void onDateSelected(String date);
    }

    DatePickerDialogFragmentEvents dpdfe

    public void setDatePickerDialogFragmentEvents(DatePickerDialogFragmentEvents dpdfe){
         this.dpdfe = dpdfe;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        String formattedDate = sdf.format(c.getTime());
        dpdfe.onDateSelected(formattedDate); //Changed
    }
}

因此,我在dialogfragment类中创建了一个名为DatePickerDialogFragmentEvents的接口,并在调用者片段中实现了该接口。在创建dialogfragment实例时从调用者片段,使用对话框片段类的setDatePickerDialogFragmentEvents方法设置片段的DatePickerDialogFragmentEvents实例。

答案 1 :(得分:2)

我已经解决了问题,感谢this topic,主题的第二次回应。
DatePickerFragment.java

@Override
    public void onDateSet(DatePicker view, int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        String formattedDate = sdf.format(c.getTime());
        // in this part I stored the selected date into the intent
        Intent i = new Intent();
        i.putExtra("selectedDate",formattedDate);
        getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);
    }

片段文件

public static final int DATEPICKER_FRAGMENT=1; // adding this line
@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_feeding, container, false);
    EditText editText = (EditText) view.findViewById(R.id.foodDeliveryDateFiled);

    editText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DialogFragment picker = new DatePickerFragment();
            picker.setTargetFragment(FeedingFragment.this, DATEPICKER_FRAGMENT);
            picker.show(getFragmentManager().beginTransaction(), "Date Picker");
        }
    });
    return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case DATEPICKER_FRAGMENT:
            if (resultCode == Activity.RESULT_OK) {
                // here the part where I get my selected date from the saved variable in the intent and the displaying it.
                Bundle bundle=data.getExtras();
                String resultDate = bundle.getString("selectedDate","error");
                EditText editText = (EditText) getView().findViewById(R.id.foodDeliveryDateFiled);
                editText.setText(resultDate);
            }
            break;
    }

答案 2 :(得分:0)

每个Java程序员的第一个响应是创建一个可观察或回调并发送它。虽然这在大多数地方都是可接受的做法,但在使用Fragments和DialogFragments时,由于状态在重新创建,旋转,暂停时会丢失,因此体验并不那么漂亮......您必然会收到您不期望的IllegalStateExceptions。

我的建议虽然有点激烈,但会将您的通信模式从一系列接口回调转移到集中式事件总线,如Otto或GreenRobot。对于任何给定的状态,它在前景和后台都很有用,并有助于解耦使用。

答案 3 :(得分:-1)

使用 ViewModel 是最好的解决方案

START TRANSACTION; 
UPDATE `db_2019`.`table_2020` SET UpdateField = UpdateValue; 
UPDATE `db_2020`.`table_2021` SET UpdateField = UpdateValue; 
...
COMMIT;

According to Android Documentation