android - How to pass selected navigation drawer item and Spinner item inside fragment of One activity to second activity? -
i have navigation drawer in first activity , upon selection on navigation drawer i'm going display fragment called spinnerfragment has spinner inside it. want pass selected navigation item , selected spinner item second activity mainactivity2.java. i'm not adding xml files, if needed added please let me know , add them.
can please me how pass data of selected in navigation drawer , in spinner???
mainactivity.java
navigationview=(navigationview)findviewbyid(r.id.navigation_view); navigationview.setnavigationitemselectedlistener(new navigationview.onnavigationitemselectedlistener() { @override public boolean onnavigationitemselected(@nonnull menuitem item) { switch (item.getitemid()) { case r.id.home_id1: fragmenttransaction=getsupportfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.maincontainer,new homefragment()); fragmenttransaction.commit(); getsupportactionbar().settitle("homefragmentyoyo"); item.setchecked(true); drawerlayout.closedrawers(); break; case r.id.home_id2: fragmenttransaction=getsupportfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.maincontainer,new spinnerfragment()); fragmenttransaction.commit(); getsupportactionbar().settitle("airtel"); item.setchecked(true); drawerlayout.closedrawers(); break; }//switch case close return false; }//navigationitemselected close
spinnerfragment.java (fragment passed in home_id_2)
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view v= inflater.inflate(r.layout.fragment_spinner, container, false); setspinnercontent(v); button button=(button)v.findviewbyid(r.id.button); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent intent= new intent(getactivity(),mainactivity2.class); startactivity(intent); } }); return v; } private void setspinnercontent(final view v) { spinner =(spinner)v.findviewbyid(r.id.spinner); final arrayadapter<charsequence> adapter=arrayadapter.createfromresource(v.getcontext(),r.array.states_names,android.r.layout.simple_spinner_item); spinner.setadapter(adapter); spinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> adapterview, view view, int i, long l) { string selected=adapterview.getitematposition(i).tostring(); context context=adapterview.getcontext(); charsequence text=selected; int duration= toast.length_short; toast toast=toast.maketext(context,text,duration); toast.show(); }
mainactivity2.java
public class mainactivity2 extends appcompatactivity { private toolbar toolbar; private tablayout tablayout; private viewpager viewpager; @override protected void oncreate(bundle savedinstancestate) { string m; super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main2); toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); viewpager = (viewpager) findviewbyid(r.id.viewpager); setupviewpager(viewpager); tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(viewpager); } private void setupviewpager(viewpager viewpager) { viewpageradapter adapter = new viewpageradapter(getsupportfragmentmanager()); adapter.addfragment(new onefragment(), "one"); adapter.addfragment(new twofragment(), "two"); adapter.addfragment(new threefragment(), "three"); viewpager.setadapter(adapter); } 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 addfragment(fragment fragment, string title) { mfragmentlist.add(fragment); mfragmenttitlelist.add(title); } @override public charsequence getpagetitle(int position) { return mfragmenttitlelist.get(position); } } }
Comments
Post a Comment