【问题标题】:Send data to fragment after is initialized初始化后向fragment发送数据
【发布时间】:2019-03-13 09:51:16
【问题描述】:

当我将数据发送到初始化选项卡时出现问题。在getData() 方法中,我收到的适配器为空,recyclerview 也为空。

TabOne one = new TabOne()
one.getData(populatedList)

错误是下一个=>

java.lang.NullPointerException: Attempt to invoke virtual method 'void OneAdapter.setData(java.util.List)' on a null object reference.

也许更好的办法是通过在片段中的捆绑发送数据或任何其他想法。

我打电话给getData(),因为这里是来自 API 的响应。

public class TabOne extends Fragment {

        private Unbinder unbinder;

        @BindView(R.id.fab)
        FloatingActionButton floatingActionButton;

        @BindView(R.id.recycler_view_recycler)
        RecyclerView recyclerView;

        private OneAdapter oneAdapter;

        private List<Response> response = new ArrayList<>();

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.tab_one, container, false);
            unbinder = ButterKnife.bind(this, view);

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
            oneAdapter = new OneAdapter(getContext(), response);

            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(oneAdapter);

            return view;
        }

        public void getData(List<Response> response){
            oneAdapter.setData(response);
        }

        @Override
        public void onDestroyView() {
            super.onDestroyView();
            unbinder.unbind();
        }

    }

【问题讨论】:

  • 使用 bundle 进行通信。
  • 捆绑是首选方式,但又取决于用例
  • 你在哪里调用 one.getData(populatedList) ?如果您从另一个活动或片段中调用它,那么您做错了
  • 是的,我从另一个片段 @Radesh 中调用它。最好的方法是什么?

标签: java android android-fragments android-viewpager android-tablayout


【解决方案1】:

您不能从其他 Activity/Fragment 调用 Fragment 的方法。

你有几种方法可以解决这个问题

A 计划(建议)

使用EventBus

1 : 像这样创建EventClass.java

public class EventClass{

    private List<FILL IT WITH YOUR OBJECT> populatedList;

    public EventClass(int populatedList) {
        this.populatedList= populatedList;
    }

    public int getPopulatedList() {
        return populatedList;
    }
}

2:使用它

在你的活动中而不是在这个

TabOne one = new TabOne()
one.getData(populatedList)

使用 EventBus 并像这样发布您的活动

EventBus.getDefault().postSticky(new EventClass(populatedList));

3 在片段中获取您的数据。将此功能添加到您的 Fragment

@Subscribe
public void onEvent(EventClass event) {
     oneAdapter.setData(event.getPopulatedList());
}

4不要忘记在 Fragmet 中注册和注销您的 EventBus

EventBus.getDefault().register(this);//add in onCreateView
//...
EventBus.getDefault().unregister(this);//add in onDestroyView

B计划

在片段中使用界面设计进行回调。您必须在 Fragment 中为 changingDataListenerimplements 等更改数据创建一个接口,并从 Activity

调用回调

C 计划(高级)

RxJavaPublishSubject 一起使用,您可以创建 Observable 以观察新数据,并且当新数据到达时您可以更新适配器。

相信我计划 A 更加简单!

【讨论】:

  • 感谢您对 viewPager.addOnPageChangeListener() 的看法?这行得通吗?
  • @Mirko 我不这么认为。一个单一的工作是使用我不建议但它有效的静态填充列表
猜你喜欢
  • 1970-01-01
  • 2018-04-14
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 2023-02-03
  • 2019-05-26
  • 2017-02-23
  • 2018-01-22
相关资源
最近更新 更多