无法从片段内的接口获取数据

Unable to get data from an interface inside a fragment

我已经实现了一个自定义接口来传递如下数据:

public interface OnLectureSelected {
    public void lectureSelected(String videoId,String lectureDescription);
}

在适配器中,然后我使用接口将数据传递给 activity 和片段:

OnLectureSelected onLectureSelected2 = (OnLectureSelected) context;
onLectureSelected2.lectureSelected(video,description);

activity能够按预期接收数据:

class Activity extends AppCompatActivity implements OnLectureSelected{
  …
 @Override
 public void lectureSelected(String videoId, String lectureDescription) {
  //I get the passed details
 }

但是,我无法使用片段获取数据:

class Fragment extends Fragment implements OnLectureSelected{
  …
 @Override
 public void lectureSelected(String videoId, String lectureDescription) {
  //Can't get the passed details
 }

从片段内部的界面获取数据的最佳方法是什么?

OnLectureSelected onLectureSelected2 = (OnLectureSelected) context;

这里这个对象只会将值传递给一个已实现的class。要将值传递给片段,您可以在 Activity class

中调用以下方法
@Override
    public void lectureSelected(String videoId, String lectureDescription) {
        //Can't get the passed details
        List<Fragment> fragments = getSupportFragmentManager().getFragments();
        if (fragments != null) {
            for (Fragment fragment : fragments) {
                if(fragment instanceof FragmentClasas) {
                    fragment.lectureSelected(video,description);
                }
            }
        }
    }