Android - Pass "edit text" element value between tabbed fragments -
i have activity multiple swipe tabbed fragments. each fragment has check-boxes, edit text , switch fields. want navigate through tabs, edit state of fields on last tab collate information within elements , save these details database.
my problem don't know how save these elements values , pass them last tab in order stored.
i quite newbie android apps hint or answer appreciated.
here how code looks far:
activity:
public class icontexttabsactivity extends appcompatactivity { private toolbar toolbar; private tablayout tablayout; private viewpager viewpager; private int[] tabicons = { r.drawable.ic_tab_favourite, r.drawable.ic_tab_call, r.drawable.ic_tab_contacts, r.drawable.ic_vector_test }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_icon_text_tabs); toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true); viewpager = (viewpager) findviewbyid(r.id.viewpager); setupviewpager(viewpager); tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(viewpager); setuptabicons(); bundle b = getintent().getextras(); string id = b.getstring("id"); toast.maketext(this, id, toast.length_long).show(); } private void setuptabicons() { tablayout.gettabat(0).seticon(tabicons[0]); tablayout.gettabat(1).seticon(tabicons[1]); tablayout.gettabat(2).seticon(tabicons[2]); tablayout.gettabat(3).seticon(tabicons[2]); tablayout.gettabat(4).seticon(tabicons[3]); } private void setupviewpager(viewpager viewpager) { viewpageradapter adapter = new viewpageradapter(getsupportfragmentmanager()); adapter.addfrag(new onefragment(), "one"); adapter.addfrag(new twofragment(), "two"); adapter.addfrag(new threefragment(), "three"); adapter.addfrag(new fourfragment(), "four"); adapter.addfrag(new fivefragment(), "five"); viewpager.setadapter(adapter); } @override public boolean onsupportnavigateup() { onbackpressed(); return true; } @override public void onbackpressed() { super.onbackpressed(); overridependingtransition(r.anim.left_to_right, r.anim.right_to_left); } class viewpageradapter extends fragmentpageradapter { private final list<fragment> mfragmentlist = new arraylist<>(); private final list<string> mfragmenttitlelist = new arraylist<>(); public viewpageradapter(fragmentmanager manager) { super(manager); } @override public fragment getitem(int position) { return mfragmentlist.get(position); } @override public int getcount() { return mfragmentlist.size(); } public void addfrag(fragment fragment, string title) { mfragmentlist.add(fragment); mfragmenttitlelist.add(title); } @override public charsequence getpagetitle(int position) { return mfragmenttitlelist.get(position); } }
tab fragment 1
public class onefragment extends fragment { private edittext mbreedname; public onefragment() { // required empty public constructor } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view v = inflater.inflate(r.layout.fragment_one, container, false); mbreedname = (edittext) v.findviewbyid(r.id.addname); return v; }
tab fragment 2
public class twofragment extends fragment { public twofragment() { // required empty public constructor } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view v = inflater.inflate(r.layout.fragment_two, container, false); return v; }
assuming tab fragment 2 last tab, how retrieve here text value "mbreedname" stored tab fragment 1?
thanks!
you can store values in activity , use callback update , query them.
try this:
on activity:
public class icontexttabsactivity extends appcompatactivity implements activitycallback{ private string mbreedtextname = ""; ... @override public void onedittextchange(string text) { this.mbreedtextname = text; } @override public string getedittextname() { return mbreedtextname; } }
the activitycallback interface:
public interface activitycallback { void onedittextchange(string text); string getedittextname(); }
then on fragment:
public class onefragment extends fragment { ... private activitycallback callback; @override public void onattach(context context) { super.onattach(context); callback = (activitycallback) context; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view v = inflater.inflate(r.layout.fragment_one, container, false); mbreedname = (edittext) v.findviewbyid(r.id.addname); button b = v.findviewbyid(r.id.accept_button); b.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { callback.onedittextchange(mbreedname.gettext().tostring()); } }); return v; } }
and same on second fragment:
public class twofragment extends fragment { ... private activitycallback callback; private string text; @override public void onattach(context context) { super.onattach(context); callback = (activitycallback) context; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view v = inflater.inflate(r.layout.fragment_two, container, false); text = callback.getedittextname(); return v; } }
Comments
Post a Comment