android - Getting the values from the different fragments from ViewPager -
i developing application fragments being generated dynamically.
private void setupviewpager(viewpager viewpager) { adapter = new viewpageradapter(getsupportfragmentmanager()); (int = 0; < sequence_no.size(); i++) { adapter.addfragment(fragmenttwo.newinstance(i,(i+1))); } viewpager.setadapter(adapter); }
now suppose there 3 fragments got generated. generated fragments have different widgets edittext, spinner etc. mannage have 1 flag notifies me 1 displayed(spinner or edittext).
i have 1 button in activity upon click need values fragments bu how getting value first fragment only.
i putting wrong sample on here know wrong keeping reference purpose.
below code on submit button click values 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 need fetching values fragments.
you can solve problem using way. create interface
, implement in each fragments. using interface
can value specific fragment.
for example , create interface
public interface igetvalue{ string getedittextvalue(); }
now, if each fragment implements interface each fragment have implements getedittextvalue()
method , can return edittext value of implemented method. use :
@override public string getedittextvalue() { return your_edit_text.gettext().tostring(); }
now, if want edittext
value fragment1
in activity, use line of code:
string valuefromfragment1= fragment1instance.getedittextvalue();
for more info, check this , this answer.
thanks :)
Comments
Post a Comment