gpt4 book ai didi

java - fragment 到 Activity 通信

转载 作者:太空宇宙 更新时间:2023-11-04 11:00:56 25 4
gpt4 key购买 nike

我会立即坦白承认我不懂接口(interface)。我相信我遵循了 https://developer.android.com/training/basics/fragments/communicating.html 中规定的流程。在“定义接口(interface)”部分中,我声明并定义了所需的接口(interface) NewDate:

public class DatePickerFragment extends DialogFragment {
NewDate mCallback;
private static final String TAG = "DatePickerFragment";
public Date date;

private DatePicker mDatePicker;

//interface implemented by DatePicker Activity
public interface NewDate{
public void UpdateDate(Date date);
}

我在调用 Activity 中实现它,如实现接口(interface)部分中所述:

public class DatePickerActivity extends SingleFragmentActivity implements DatePickerFragment.NewDate {
private final String TAG = "DatePickerActivity";
private static final int REQUEST_DATE = 0;

@Override
protected Fragment createFragment() {
Log.d(TAG, "Entered createFragment");
Intent intent = getIntent();
Date date = (Date) intent.getSerializableExtra(EXTRA_DATE);
Log.d(TAG,"date = " + date);
UUID id = (UUID) intent.getSerializableExtra(EXTRA_ID);
Log.d(TAG, "crime ID is " + id);
return DatePickerFragment.newInstance(date);
}

public void UpdateDate(Date date) {
Log.d(TAG, "Entered UpdateDate");
Log.d(TAG, "Date is " + date);
Toast.makeText(getApplicationContext(),"Clicked OK to update date!", Toast.LENGTH_SHORT).show();
return;
}
}

忽略返回日期可能错误的事实,问题是代码永远不会在这里实现。

如图所示,当用户单击“确定”按钮将事件传递给父 DatePickerActivity 时,将调用 DatePickerFragment 中的以下方法,并在 DatePickerFragment 附加到 DatePickerActivity 时执行 onAttach 方法:

    public class DatePickerFragment extends DialogFragment {

...

private void sendResult(int resultCode, Date date){
Log.d(TAG, "Entered sendResult");
Log.d(TAG, "resultCode is " + resultCode);
Log.d(TAG, "Date passed in to sendResult is " + date);

if (getTargetFragment() == null) {
if (mCallback != null){
mCallback.UpdateDate(date);
return;
} else {
Log.d(TAG, "mCallback is null");
return;
}

...

@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d(TAG, "Entered onAttach");
try {
Log.d(TAG, "Trying to create new mCallback");
mCallback = (NewDate) date;
Log.d(TAG, "mCallback is " + mCallback);
} catch (ClassCastException e){
throw new ClassCastException(date.toString() + " must implement NewDate");
}

以下是我运行代码后的LogCat输出结果:

DatePickerActivity: Entered createFragment
DatePickerActivity: date = Mon Oct 23 15:24:38 CDT 2017
DatePickerActivity: crime ID is a205fce6-9a38-4c40-9dce-ba651c281752
DatePickerFragment: Entered newInstance
DatePickerFragment: Date is Mon Oct 23 15:24:38 CDT 2017
DatePickerFragment: id is 16842960
DatePickerFragment: Entered onAttach
DatePickerFragment: Trying to create new mCallback
DatePickerFragment: mCallback is null
DatePickerFragment: Entered onCreateView
DatePickerFragment: Date passed to DatePickerFragment is: Mon Oct 23 15:24:38 CDT 2017
DatePickerFragment: Set date in calendar
DatePickerFragment: year is 2017
DatePickerFragment: month is 9
DatePickerFragment: day is 23
DatePickerFragment: Entered onActivityCreated
DatePickerFragment: Entered onStart
DatePickerFragment: Entered onResume
DatePickerFragment: You clicked OK!
DatePickerFragment: onClick: year is 2017
DatePickerFragment: onClick: month is 9
DatePickerFragment: onClick: day is 31
DatePickerFragment: Dialog date is Tue Oct 31 00:00:00 CDT 2017
DatePickerFragment: Entered sendResult
DatePickerFragment: resultCode is -1
DatePickerFragment: Date passed in to sendResult is Tue Oct 31 00:00:00 CDT 2017
DatePickerFragment: mCallback is null

如果您对齐代码的 Log.d 消息,您会注意到 DatePickerActivity 中的 UpdateDate 永远不会被访问,因为 mCallback 为 null。本以为onAttach后mCallback就不会为null了,结果还是为null。我读过其他网页,例如http://simpledeveloper.com/how-to-communicate-between-fragments-and-activities/http://techblogon.com/communication-between-activity-and-fragment-example/ ,这让我认为我已经满足了接口(interface)要求。但是,我显然没有。

那么,谁能解释一下我在这些网页上错过了什么,或者提供一个链接来解释我错过了什么?

最佳答案

您需要将 mCallback 分配给托管 fragment 并实现 DatePickerFragment.NewDate 接口(interface)的 Activity:

@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d(TAG, "Entered onAttach");
try {
Log.d(TAG, "Trying to create new mCallback");
//modified:
if (context instanceof DatePickerActivity){
mCallback = (DatePickerActivity) context;
}
Log.d(TAG, "mCallback is " + mCallback);
} catch (ClassCastException e){
throw new ClassCastException(date.toString() + " must implement NewDate");
}
}

关于java - fragment 到 Activity 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46900069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com