0

I'm getting the response through OK-HTTP request by calling API which contains some students data, but I'm not able to send that data from my fragment which is in view pager and that view pager with tab layout in bottom sheet dialog fragment

public static class AddBottomSheetDialog extends BottomSheetDialogFragment {

    private ViewPager viewPager;
    private TabLayout tabLayout;
    private ImageView closeImage;

    public static AddBottomSheetDialog newInstances(){
        return new AddBottomSheetDialog();
    }

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

        tabLayout  = view.findViewById(R.id.tablayout);
        viewPager = view.findViewById(R.id.viewpager);
        closeImage = view.findViewById(R.id.iv_close);
        btnApply = view.findViewById(R.id.filter_btn);

        closeImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDialog().dismiss();
            }
        });

        btnApply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDialog().dismiss();
            }
        });

        SampleTestTabsAdapter adapter = new SampleTestTabsAdapter(getChildFragmentManager(),4);
        adapter.addFragment("By\n Course", new SampleCourseWiseFragment());
        adapter.addFragment("By\nStd",new SampleStandardWiseFragment());

        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

        return view;
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override public void onShow(DialogInterface dialogInterface) {
                BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialogInterface;
                setupFullHeight(bottomSheetDialog);
            }
        });
        return dialog;
    }

    private void setupFullHeight(BottomSheetDialog bottomSheetDialog) {
        FrameLayout bottomSheet = (FrameLayout) bottomSheetDialog.findViewById(R.id.design_bottom_sheet);
        BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
        ViewGroup.LayoutParams layoutParams = bottomSheet.getLayoutParams();

        int windowHeight = getWindowHeight();
        if (layoutParams != null) {
            layoutParams.height = windowHeight;
        }
        bottomSheet.setLayoutParams(layoutParams);
        behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
    }

    private int getWindowHeight() {
        // Calculate window height for fullscreen use
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.heightPixels-100;
    }
}

now the fragment code that I've written that is shown as below:

@Override
public void onOkHttpSuccess(int requestId, int statusCode, String response) {
    Log.e(">>>", "" + response);

    if (response == null) {
        return;
    }

    switch (requestId){
        case CODE_STUDENT_STANDARD:
            Log.e("Standards >>", "" + response);
            final Gson studentStandardList = new Gson();
            try {
                modelClassForStandardList = studentStandardList.fromJson(response, ModelClassForStandard.class);
                if (modelClassForStandardList.getStandards() != null
                        && modelClassForStandardList.getStandards().size() > 0) {
                    mALstandard = modelClassForStandardList.getStandards();
                    setCourseStandardListAdapter();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;
        case CODE_INQUIRY_FILTER:
            Log.e("standard wise filter",""+response);
            final Gson standardwisefilter = new Gson();
            try{
                mALinquirydetails = new ArrayList<>();
                modelClassForInquiryDetails = standardwisefilter.fromJson(response,ModelClassForInquiryDetails.class);
                if(modelClassForInquiryDetails.getInquiries()!=null && modelClassForInquiryDetails.getInquiries().size()>0){
                    mALinquirydetails = modelClassForInquiryDetails.getInquiries();
                    MyBottomsheetclicklistner myBottomsheetclicklistner = (MyBottomsheetclicklistner) getActivity();
                    myBottomsheetclicklistner.onReturnValue(mALinquirydetails);
                    Toast.makeText(getActivity(), ""+mALinquirydetails.get(0).getFname(), Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getActivity(), "Data not found Here According to : "+strSelectedStandardIds, Toast.LENGTH_SHORT).show();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            break;
    }
}

now my question is I'm getting these data in array list but how a button would know that which kind of data are coming from fragment ? Can we get control over the button which is bottom sheet dialog fragment or in another activity ? Is this possible to hit the apply button via fragment in android studio ?

viru
  • 1
  • 4
  • Try to use Interface to pass data from fragment to bottomsheet. Is bottomsheet inside activity? – urvi joshi Feb 06 '20 at 13:07
  • No bottomsheet is not in an activity. That's the problem. and one thing is that if i am able to pass the data and then when I click on apply button how a button would know that the data coming from which fragment because I've two fragments here in bottomsheet dialog fragment with viewpager and tablayout. – viru Feb 06 '20 at 13:15
  • Make an interface class, Pass data using interface class on click, with data pass one flag in which you can define your fragment. So you will also able to know from which fragment data coming. – urvi joshi Feb 06 '20 at 13:24

0 Answers0