I have a FRAGMENT with a viewpager with 7 other fragments. These fragments are used to enter user details. In the final fragment, there is a "submit" button where I need to get all the user details entered in the other fragments. How can I do this
I saw this question which didnt help. So please dont mark this question duplicate.
The base fragment:
public class Register_Layout extends Fragment implements ViewPager.OnPageChangeListener{
public Register_Layout(){}
static ViewPager viewPager1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_register_layout, container, false);
viewPager1 = (ViewPager) view.findViewById(R.id.view_pager1);
viewPager1.setOffscreenPageLimit(7);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
viewPager1.setAdapter(new MyAdapter(fragmentManager));
return view;
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
if(position==0)
{
fragment = new Register();
}
if(position==1)
{
fragment = new Register_Page2();
}
if(position==2)
{
fragment = new Register_Page3();
}
if(position==3)
{
fragment = new Register_Page4();
}
if(position==4)
{
fragment = new Register_Page5();
}
if(position==5)
{
fragment = new Register_Page6();
}
if(position==6)
{
fragment = new Register_Page7();
}
return fragment;
}
@Override
public int getCount() {
return 7;
}
}
The final fragment where I should get data of all other fragments:
public class Register_Page7 extends Fragment {
public Register_Page7(){
}
EditText editText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_register_page7, container, false);
Button Previous = (Button) view.findViewById(R.id.Previous7);
Button Regiter = (Button) view.findViewById(R.id.Submit);
Regiter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setMessage("Are you sure you want to submit?");
alertDialogBuilder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
alertDialogBuilder.setNegativeButton("No,Let me check the details again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
Previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Register_Layout().viewPager1.setCurrentItem(5);
}
});
return view;
}
}
You will probably want to use the fragment manager to get all of the instantiated fragments, and loop through them to get their relevant data.
You could create a subclass of Fragment
that all of the fragments would inherit from. This subclass would have a getText
method(or some other type of public method), that gets the text from the EditText
view.
For other types of data(like switches), you would have similar functions to get the data from the fragment.
Edited
So you could create a new fragment that is derived from Fragment
.
public class BaseEntryFragment extends Fragment {
public BaseEntryFragment() {}
public String getEntryText() {}
}
Then you take your existing fragments and extend your new fragment..
public class Register_Page7 extends BaseEntryFragment {
private EditText _editText;
public Register_Page7() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
//other create view code
_editText = (EditText)FindViewById(R.id.edit_text);
}
@Override
public String getEntryText() {
return _editText.getText();
}
}
Then with your submit..
List<Fragment> fragments = getActivity().getSupportFragmentManager().getFragments();
for(Fragment frag : fragments) {
// do type checks here
BaseEntryFragment f = (BaseEntryFragment)frag;
String someValue = f.getEntryText();
}
Please note, my Java is really really rusty, so it may not be the perfect syntax. This is just some code to get you started and not intended to be copied and pasted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With