android - How do I link child fragments to the MainActivity? -
i'm building simple nba app since i'm new this.
the mainactivity has listview 2 items. western teams , eastern teams. when user taps on item, shows teams of conference. these teams stored array items in strings.xml file.
here mainactivity:
public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new placeholderfragment()) .commit(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { arrayadapter<string> mconferenceadapter; public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { string[] conferencearray = { "western conference", "eastern conference" }; list<string> conferencelist = new arraylist<string>(arrays.aslist(conferencearray)); mconferenceadapter = new arrayadapter<string>( getactivity(), // current context (this activity) r.layout.list_item_conferences, // name of layout id. r.id.list_item_conference_textview, // id of textview populate. conferencelist); view rootview = inflater.inflate(r.layout.fragment_main, container, false); // reference listview, , attach adapter it. listview listview = (listview) rootview.findviewbyid(r.id.listview_conferences); listview.setadapter(mconferenceadapter); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int position, long l) { string forecast = mconferenceadapter.getitem(position); intent intent = new intent(getactivity(), westernconference.class) .putextra(intent.extra_text, forecast); startactivity(intent); } }); return rootview; } }
the issue have no matter selection choose shows western teams.
this snippet westconference.java:
public static class placeholderfragment extends fragment { arrayadapter<string> mconferenceadapter; public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // create dummy data listview. here's sample weekly forecast string[] teams = getresources().getstringarray(r.array.westteams); list<string> conferencelist = new arraylist<string>(arrays.aslist(teams)); // have dummy forecast data, create arrayadapter. // arrayadapter take data source (like our dummy forecast) , // use populate listview it's attached to. mconferenceadapter = new arrayadapter<string>( getactivity(), // current context (this activity) r.layout.list_item_conferences, // name of layout id. r.id.list_item_conference_textview, // id of textview populate. conferencelist); view rootview = inflater.inflate(r.layout.fragment_main, container, false); // reference listview, , attach adapter it. listview listview = (listview) rootview.findviewbyid(r.id.listview_conferences); listview.setadapter(mconferenceadapter); return rootview; } }
how can make if click west, loads teams, , if click east, loads eastern teams?
Comments
Post a Comment