0

I have a ViewPager with 3 fragments in it, each fragment has a next and prev. button, and you can also swipe left and right to navigate through fragments. and although I'm able to get the input from each fragment when the next button is clicked, I sill can't figure out a way to get the input when the user scrolls between fragments.

this is the code in my first fragment:

    public class signupfrag1 extends Fragment {
    private TextInputEditText fName, lName;
    public Spinner spncountry, spncity;
    private Button next1;
    private String vfName, vlName, vcountry, vcity;
    private String method;
    signupfrag1Listener activityCommander;

    public interface signupfrag1Listener{
        public void getDataFromFrag1(String fName, String lName, String countryName, String cityName);
    }

   @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try{
            activityCommander = (signupfrag1Listener) context;
        }catch (ClassCastException e){
            throw new ClassCastException(context.toString());
        }

    }
@Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.signupfrag1, container, false);
     fName = (TextInputEditText) view.findViewById(R.id.edtFName);
     lName = (TextInputEditText) view.findViewById(R.id.edtLName);
     spncountry = (Spinner) view.findViewById(R.id.cmbCountry);
     spncity = (Spinner) view.findViewById(R.id.cmbCities);
     next1 = (Button) view.findViewById(R.id.btnNext2);

next1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
             vfName = fName.getText().toString();
             vlName = lName.getText().toString();

             if(vfName.equals("") || vfName.length()==1){
                 fName.setError("please enter a correct first name!");
             }
             else if(vlName.equals("") || vlName.length()==1){
                 lName.setError("Please enter a correct first name!");
             }
             else
                startFrag2(view);
         }
     });}

public void startFrag2(View view){
            activityCommander.getDataFromFrag1(vfName, vlName, vcity, vcountry);

        }

this is the code from my main activity:

public class Main2Activityforfragments extends AppCompatActivity implements signupfrag1.signupfrag1Listener, signupfrag2.signupfrag2Listener {
    private ViewPager fragsViewPager;
    private SectionsPagerAdapter1 sectionsPagerAdapter1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2_activityforfragments);

fragsViewPager = (ViewPager) findViewById(R.id.fragsViewPager);
    sectionsPagerAdapter1 = new SectionsPagerAdapter1(getSupportFragmentManager());
    fragsViewPager.setAdapter(sectionsPagerAdapter1);


}


//this get called when the next button is clicked
@Override
public void getDataFromFrag1(String fName, String lName, String cityName, String countryName) {
    if(fragsViewPager.getCurrentItem() == 0){
        fragsViewPager.setCurrentItem(1);}

}
amm965
  • 399
  • 5
  • 14

1 Answers1

0

if you are in the first fragment then create ref. of interface and call its method and pass the values on Click what you want and it is needed to implement Two interface in SecondFragment. call its method in the First Fragment. you can create interface Two in SecondFragment

Two Second=new SecondFragment();  // it will be called on click of firstFragment which pass the value.
Second.two("Value one","Value two");

interface Two{
void two(String value,String value);   // implement this inteface in SecondFragment and call it in FirstFragment
}

When you reach to second you get the value from the first fragment and continue with the third fragment as same.

Farhana Naaz Ansari
  • 7,524
  • 26
  • 65
  • 105