How to collect value/data from Fragment

oke staright to the point, i'm using ViewPager, and i have more than one fragments. Each fragment has it own form

+=================+
|    Fragment1    |
+=================+
| Name (EditText) |
===================
| ID No (EditText)|
===================

+=================+ 
|    Fragment2    |
+=================+ 
| Name (EditText) |
=================== 
| ID No (EditText)|
===================

I have a single button in FragmentActivity, not in Fragment. How do I add/collect all of these EditTexts into a list from fragmentActivity?

thanks for any help

Answers


Give a unique tag for every fragment, for an example fragment1 has tag "frag1" and fragment2 has tag "frag2", in your FragmentActivity, you can access the fragments using their tags as below:

Fragment1 fragment1 = (Fragment1)getFragmentManager().findFragmentByTag("frag1");
Fragment2 fragment2  = (Fragment2)getFragmentManager().findFragmentByTag("frag2");

Now you can access the edittext in fragment1/fragment2 as,

EditText editText1 = (EditText) fragment1.getView().findViewById(R.id.editText);

then, You can get the text as, editText1.getText();


Another possibility is if you don't want to use getView and findViewById and that (I don't really know which one is more performant, if anyone knows anything about it I'd be happy to get it explained :)):

class MyFirstFragment extends Fragment { 
    ... 
    private EditText someEditText; 
    ...
    public Object doSomethingAndGetMeSomething() {
        return somePreviouslyDefinedData;
    }
}

You simply cast the fragment you've received by the tag directly into this java class:

MyFirstFragment firstFrag = (MyFirstFragment) getFragmentManager().findFragmentByTag("frag1");

and call on some functions you've defined to let it perform any task you want.

Not sure which way is better in means of performance, as in this one you will have to keep reference to your view when creating the view, in the other you simply search for it..?


Need Your Help

Alternatives to using CSV in JMeter (for generating usernames)

jmeter beanshell

I have a JMeter Test Plan with following structure

Apple receipt validation for iOS7. Add libcrypto.a and libssl.a libraries to XCode project

ios openssl in-app-purchase asn.1 receipt

I need to validate in-app purchase receipts. Apple has released a new approach for receipts, now the receipts can be stored on device in NSBundle. It requires OpenSSL and ASN1 be added to XCode pro...