0 Replies - 710 Views - Last Post: 08 December 2017 - 09:49 AM

#1 CplUSMC   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 27-February 13

How to get data from a Fragment which is inside a DialogFragment

Posted 08 December 2017 - 09:49 AM

I have a DialogFragment that is using a FragmentPagerAdapter to display tabs. Each tab has a different Fragment that uses a RecyclerView. I can get what item is clicked passed to the Fragment, but how can I get the data from the Fragment to the DialogFragment in order to pass it to the calling Activity?

TabDialog extends DialogFragment:
public class TabDialog extends DialogFragment {

public interface OnCompleteListener{
    void onCompleteListener(String test);
}

private OnCompleteListener mListener;

TabLayout tabLayout;
ViewPager viewPager;
FragmentTabPositive tabPositive = new FragmentTabPositive();
FragmentTabNegative tabNegative = new FragmentTabNegative();
TabAdapter adapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    getDialog().setCanceledOnTouchOutside(true);
    View rootView = inflater.inflate(R.layout.dialog_layout, container, false);
    tabLayout = (TabLayout) rootView.findViewById(R.id.tabLayout1);
    viewPager = (ViewPager) rootView.findViewById(R.id.masterViewPager);
    adapter = new TabAdapter(getChildFragmentManager());
    adapter.addFragment("Positive", tabPositive);
    adapter.addFragment("Negative", tabNegative);
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);

    return rootView;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try{
        this.mListener = (OnCompleteListener)context;
    } catch(ClassCastException e){
        throw new ClassCastException(context.toString() +
                " must Implement OnCompleteListener");
    }

}



TabAdapter extends FragmentPagerAdapter
public class TabAdapter extends FragmentPagerAdapter {

List<Fragment> mFragmentCollection = new ArrayList<>();
List<String> mTitleCollection = new ArrayList<>();

public TabAdapter(FragmentManager fm){
    super(fm);
}

public void addFragment(String title, Fragment fragment){

    mTitleCollection.add(title);
    mFragmentCollection.add(fragment);
}

@Override
public CharSequence getPageTitle(int position){
    return mTitleCollection.get(position);
}

@Override
public int getCount(){
    return mFragmentCollection.size();
}

@Override
public Fragment getItem(int position) {
    return mFragmentCollection.get(position);
}



Inside the onCreateView within FragmentTabPos
popupAdapter = new PopupAdapter(view.getContext(), list);
    recyclerView.setAdapter(popupAdapter);

    popupAdapter.setmListener(new PopupAdapter.OnPointClickedListener() {
        @Override
        public void OnItemClicked(String test) {
            //This works
            Toast.makeText(FragmentTabPositive.this.getContext(), test + " Inside FragTabPos", Toast.LENGTH_LONG).show();
        }
    });



Is This A Good Question/Topic? 0
  • +

Page 1 of 1