0
我想從一個fragmentdialog發送數據,我將DatePicker實現爲一個常規片段。我正在通過使用接口來完成此操作。 我得到一個錯誤,我不知道我做錯了什麼。使用onClick方法中的setDatePickerEvents錯誤是「無法解決」。Android - 從DialogFragment到片段的數據
FirstFragment:
public class FirstFragment extends Fragment implements
DateFragment.DatePickerEvent {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
EditText date= (EditText) rootView.findViewById(R.id.datePicker);
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogFragment picker = new DatePickerFragment();
picker.setDatePickerEvents(FirstFragment.this);
picker.show(getFragmentManager(), "datePicker");
}
});
return rootView;
}
@Override
public void onDateSeletcted(String date) {
String sDate= date;
EditText dp = (EditText) getView().findViewById(R.id.datePicker);
dp.setText(sDate);
}
}
DatePickerFragment:
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
int year, month, day;
public interface DatePickerEvent{
void onDateSeletcted(String date);
}
DatePickerEvent dpe;
public void setDatePickerEvents(DatePickerEvent dpe){
this.dpe = dpe;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate = sdf.format(c.getTime());
dpe.onDateSeletcted(formattedDate);
}
}
哦,這是一個錯誤,當我在這篇文章中寫的代碼。這並不能解決錯誤 – John