Reputation: 47
I try to view tablayout with dynamic inside activity, data from server.
Data from server is retrieved successfully.
Inside Activity:
onCreate:
setupTab();
loadBarang();
setupTab:
private void setupTab(){
adapterViewPagerBarangOrder = new AdapterViewPagerBarangOrder(getSupportFragmentManager());
viewPager.setAdapter(adapterViewPagerBarangOrder);
tabLayout.setupWithViewPager(viewPager);
}
request to server
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;
}
}
Fragment
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;
}
}
Check size of listBarang inside fragment.newInstance not null and was not empty
but check inside fragment.oncreate arguments always null
Upvotes: 0
Views: 779
Reputation: 878
Replace
listBarang = (List<Barang>) getArguments().getSerializable("ARG_LIST_BARANG");
with
listBarang = (List<Barang>) getArguments().getSerializable(ARG_LIST_BARANG);
Key should be same when setting and getting arguments.
Upvotes: 2
Reputation: 200020
You're using
getSerializable("ARG_LIST_BARANG")
But putting in the Serializable by using
putSerializable(ARG_LIST_BARANG, (Serializable) listBarang);
Where the value of ARG_LIST_BARANG
is "create_order_arg_list_barang"
- you're not using the same key on both sides. You should remove the quotes in getSerializable
to actually use your variable:
listBarang = (List<Barang>) getArguments().getSerializable(ARG_LIST_BARANG);
Upvotes: 1