我尝试查看具有动态内部活动(来自服务器的数据)的布局。
已成功检索到服务器中的数据。
内部活动:
onCreate:
setupTab();
loadBarang();
setupTab:
private void setupTab(){
adapterViewPagerBarangOrder = new AdapterViewPagerBarangOrder(getSupportFragmentManager());
viewPager.setAdapter(adapterViewPagerBarangOrder);
tabLayout.setupWithViewPager(viewPager);
}
请求服务器
private void loadBarang(){
...
// success
adapterViewPagerBarangOrder.setGroupBarangs(listGroupBarang);
...
}
AdapterViewPager:
public class AdapterViewPagerBarangOrder extends FragmentStatePagerAdapter {
List<CreateOrderFragment> fragments;
List<GroupBarang> groupBarangs;
public AdapterViewPagerBarangOrder(@NonNull FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
this.fragments = new ArrayList<>();
this.groupBarangs = new ArrayList<>();
}
public void setGroupBarangs(List<GroupBarang> groupBarangs) {
this.groupBarangs = groupBarangs;
for (GroupBarang groupBarang: groupBarangs) {
fragments.add(CreateOrderFragment.newInstance(groupBarang.getListBarang()));
}
notifyDataSetChanged();
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragments.size()>0? fragments.get(position) : null;
}
@Override
public int getCount() {
return fragments.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return groupBarangs.size()>0? groupBarangs.get(position).getGroupName() : null;
}
}
片段
public class CreateOrderFragment extends BaseFragment {
private static final String ARG_LIST_BARANG = "create_order_arg_list_barang";
private AdapterBarangOrder adapterBarangOrder;
private List<Barang> listBarang;
private EmptyRecyclerView mRecyclerView;
private CartManager cartManager;
public CreateOrderFragment() {
listBarang = new ArrayList<>();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
listBarang = (List<Barang>) getArguments().getSerializable("ARG_LIST_BARANG");
}
// HERE CHECK listBarang ALWAYS NULL
// ANY HELP?
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_create_order, container, false);
cartManager = new CartManager(getContext());
adapterBarangOrder = new AdapterBarangOrder(getContext());
mRecyclerView = view.findViewById(R.id.create_order_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false));
mRecyclerView.setAdapter(adapterBarangOrder);
adapterBarangOrder.setListBarang(listBarang);
return view;
}
public static CreateOrderFragment newInstance(List<Barang> listBarang){
CreateOrderFragment fragment = new CreateOrderFragment();
Bundle args = new Bundle();
args.putSerializable(ARG_LIST_BARANG, (Serializable) listBarang);
// HERE CHECK listBarang NOT NULL and NOT EMPTY
fragment.setArguments(args);
return fragment;
}
}
检查fragment.newInstance内部的listBarang的大小不为null且不为空
但要检查fragment.oncreate参数内部始终为空
答案 0 :(得分:2)
替换
listBarang = (List<Barang>) getArguments().getSerializable("ARG_LIST_BARANG");
与
listBarang = (List<Barang>) getArguments().getSerializable(ARG_LIST_BARANG);
设置和获取参数时,键应相同。
答案 1 :(得分:1)
您正在使用
getSerializable("ARG_LIST_BARANG")
但是通过使用
putSerializable(ARG_LIST_BARANG, (Serializable) listBarang);
在ARG_LIST_BARANG
的值为"create_order_arg_list_barang"
的情况下-您没有在两侧使用相同的键。您应该删除getSerializable
中的引号以实际使用您的变量:
listBarang = (List<Barang>) getArguments().getSerializable(ARG_LIST_BARANG);