0

I am developing an application where the fragments are being generated dynamically.

private void setupViewPager(ViewPager viewPager) {
    adapter = new ViewPagerAdapter(getSupportFragmentManager());
    for (int i = 0; i < sequence_no.size(); i++) 
    {
        adapter.addFragment(FragmentTwo.newInstance(i,(i+1)));
    }
    viewPager.setAdapter(adapter);
}

Now suppose there does three fragments got generated. The generated fragments do have different widgets like edittext, spinner etc. which i do mannage as i have one flag which notifies me which one to be displayed(spinner or edittext).

I have one button in activity upon whose click i need to get the values from all the fragments bu some how I am getting the value from first fragment only.

I am putting a wrong sample over here as I know its wrong but I am keeping for reference purpose.

below is the code on submit button click where values is an array list.

if (FragmentTwo.ed_complain != null) 
    {
        values.add(FragmentTwo.ed_complain.getText().toString());
    }
    if (FragmentTwo.cb_complain != null) 
    {
        values.add(FragmentTwo.cb_complain.getText().toString());
    }
    if (FragmentTwo.spinner_complain != null && FragmentTwo.spinner_complain.getSelectedItem() != null) 
    {
        values.add(FragmentTwo.spinner_complain.getSelectedItem().toString());
    }

Please suggest me what I need to do for fetching all the values from all the fragments.

4 Answers4

2

You can solve this problem using this way. Create an Interface and implement it in each fragments. Now using this Interface you can get value from specific fragment.

For Example , you create an Interface

public interface IGetValue{
String getEditTextValue();
}

Now, if each Fragment implements this interface then each fragment have to implements this getEditTextValue() method and you can return EditText value of that implemented method. just use :

@Override
public String getEditTextValue() {

    return YOUR_EDIT_TEXT.getText().toString();
}

Now, if you want to get EditText value from Fragment1 in your activity, just use this line of code:

String valueFromFragment1= fragment1Instance.getEditTextValue();

For more info, check this and this answer.

Thanks :)

Community
  • 1
  • 1
Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87
0

Use Bundle

Put variable to Bundle:-

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putString(key, value);
fragment.setArguments(bundle);

Retrieve variable from bundle :-

Bundle bundle = this.getArguments();
String myTxt = bundle.getInt(key, defaultValue);
Arshid KV
  • 9,631
  • 3
  • 35
  • 36
  • as I am having a single Fragment i.e FragmentTwo I am only creating its instances. Now suppose instance 1 do have spinner i have selected one value and I switched to another instance which is having edittext where I typed Hello. Now pressing a submit button(defined in Activity) should bring out both values of spinner as well as edittext. –  Apr 25 '16 at 04:17
  • Retrieve bundle value from activity – Arshid KV Apr 25 '16 at 04:19
0

In your code, where you are adding fragment to adapter, store that fragment in an ArrayList

ArrayList<Fragment> allMyFrags=new ArrayList<Fragment>();
private void setupViewPager(ViewPager viewPager) {
    adapter = new ViewPagerAdapter(getSupportFragmentManager());
    for (int i = 0; i < sequence_no.size(); i++) 
    {
        Fragment mFrag=FragmentTwo.newInstance(i,(i+1));
        allMyFrags.add(mFrag);
        adapter.addFragment(mFrag);
    }
    viewPager.setAdapter(adapter);
}

later in your code whenever you wanted get data, put that array in a for loop and get your desired Data

Omid Heshmatinia
  • 5,089
  • 2
  • 35
  • 50
0

Thank you guys:) Here I am sharing the solution which I have got with the help of the answer of Md. Sajedul Karim and Smartiz.

for(int i=0;i<allMyFrags.size();i++)
    {
        FragmentTwo fragment = (FragmentTwo) adapter.getItem(i);    
        Log.e("Value",fragment.getEditTextValue());
        values.add(fragment.getEditTextValue());
    }

where getEdittextValue() is the method from interface implemented in FragmentTwo().