Ashutosh Maheshwari
Ashutosh Maheshwari

Reputation: 59

Extract data from multiple fragments from single button

I am making a contact app. It has three fragments, scrollview, that has edit texts. Also the activity has one save button in menu item. I wanna to save the data of all three fragments by just that single save button. My UI looks something like this

Add Records One Fragment

How can I extract data from all three fragments to my container activity.

This is my code of Menu Item SAVE in Container Activity

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.discard :
            break;
        case R.id.save:
            Personal_Details p1 = new Personal_Details();
            Proffesional_Details p2 = new Proffesional_Details();
            Social s = new Social();
            Person p = new Person(p1.getName(),p1.getLastName(),p1.getPhone(),p1.getEmail(),p1.getDOB(),
                    p2.getCName(),p2.getTitle(),p2.getIM(),p2.getwebsite(),p2.getOfficeA(),
                    s.getNickName(),s.getIntro(),s.getFb(),s.getTwitter(),s.getInsta());
            dbHelper Database = new dbHelper(this,null,null,1);
            Database.addPerson(p);
            break;
    }
    return true;
}
}

This is my code in one of the three identical fragments PERSONAL_DETAILS :

public class Personal_Details extends Fragment {

EditText name, lastname, phoneNumber,email,dob;
public Personal_Details() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_personal__details, container, false);

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    initialize();
}

private void initialize() {
    name= (EditText)getActivity().findViewById(R.id.name);
    lastname= (EditText)getActivity().findViewById(R.id.lastName);
    phoneNumber= (EditText)getActivity().findViewById(R.id.phoneNumber);
    email= (EditText)getActivity().findViewById(R.id.emailAddress);
    dob= (EditText)getActivity().findViewById(R.id.DOB);
}

public String getName(){
    if(name.getText().toString()!=null)
        return name.getText().toString();
    else
        return "";
}

public String getLastName(){
    if(lastname.getText().toString()!=null)
        return lastname.getText().toString();
    else
        return "";
}

public String getEmail(){
    if(email.getText().toString()!=null)
        return email.getText().toString();
    else
        return "";
}

public String getPhone(){
    if(phoneNumber.getText().toString()!=null)
        return phoneNumber.getText().toString();
    else
        return "";
}

public String getDOB(){
    if(dob.getText().toString()!=null)
        return dob.getText().toString();
    else
        return "";
}
}

How can I save the info from these fragments to My Container Activity by Save Button?

EDIT : Here is my Adapter for Scroll View

`class MyAdapter extends FragmentStatePagerAdapter{

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

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;
    switch (position){
        case 0:
            fragment = new Personal_Details();
            break;
        case 1:
            fragment = new Proffesional_Details();
            break;
        case 2 :
            fragment = new Social();
            break;
        default:break;
    }
    return fragment;
}

@Override
public int getCount() {
    return 3;
}

@Override
public CharSequence getPageTitle(int position) {
    switch (position){
        case 0 : return "Personal";
        case 1 : return "Professional";
        case 2 : return "Social";
        default:return null ;
    }
}

}`

Upvotes: 4

Views: 435

Answers (1)

jaibatrik
jaibatrik

Reputation: 7533

You can get all your Fragments from the FragmentStatePagerAdapter and call the getter methods from them. You can save references to the Fragments in your FragmentStatePagerAdapter in instantiateItem method and remove them in destroyItem method.

Example code -

public class MyPagerAdapter extends FragmentStatePagerAdapter {
    SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

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

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

    @Override
    public Fragment getItem(int position) {
        return MyFragment.newInstance(...); 
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);
    }

    public List<Fragment> getAllFragments() {
        List<Fragment> list = new List<Fragment>();

        for(int i = 0; i < sparseArray.size(); i++) {
           int key = sparseArray.keyAt(i);
           // get the object by the key.
           list.add(sparseArray.get(key));
        }

        return list;
    }
}

Upvotes: 1

Related Questions