初始化后向片段发送数据
Send data to fragment after is initialized
我在向初始化选项卡发送数据时遇到问题。在方法 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();
}
}
您不能从另一个 Activity/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 : 使用它
在你的 activity 而不是这个
TabOne one = new TabOne()
one.getData(populatedList)
像这样使用 EventBus 和 post 你的事件
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中进行回调。您必须为您的 Fragment 中的 changingDataListener
和 implements
等更改数据创建一个接口,并从 Activity
计划 C(高级)
使用 RxJava 和 PublishSubject
你可以创建 Observable 来观察新数据,当新数据到达时你可以更新你的适配器。
相信我 计划 A 更简单!
我在向初始化选项卡发送数据时遇到问题。在方法 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();
}
}
您不能从另一个 Activity/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 : 使用它
在你的 activity 而不是这个
TabOne one = new TabOne()
one.getData(populatedList)
像这样使用 EventBus 和 post 你的事件
EventBus.getDefault().postSticky(new EventClass(populatedList));
3 抓取片段中的数据。将此功能添加到您的 Fragment
@Subscribe
public void onEvent(EventClass event) {
oneAdapter.setData(event.getPopulatedList());
}
4 不要忘记在 Fragmet
中注册和注销您的 EventBusEventBus.getDefault().register(this);//add in onCreateView
//...
EventBus.getDefault().unregister(this);//add in onDestroyView
B计划
使用接口设计在fragment中进行回调。您必须为您的 Fragment 中的 changingDataListener
和 implements
等更改数据创建一个接口,并从 Activity
计划 C(高级)
使用 RxJava 和 PublishSubject
你可以创建 Observable 来观察新数据,当新数据到达时你可以更新你的适配器。
相信我 计划 A 更简单!