How do I link child fragments to the MainActivity?

I'm building a very simple NBA App since I'm new to this.

The MainActivity has a ListView with two items. Western teams and Eastern Teams. When the user taps on an item, it shows the teams of that conference. These teams are stored as array items in the strings.xml file.

Here is the 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 the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a 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(), // The current context (this activity) R.layout.list_item_conferences, // The name of the layout ID. R.id.list_item_conference_textview, // The ID of the textview to populate. conferenceList); View rootView = inflater.inflate(R.layout.fragment_main, container, false); // Get a reference to the ListView, and attach this adapter to 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 I have now is that no matter which selection I choose it only shows the Western teams.

This is a snippet from WestConference.java:

public static class PlaceholderFragment extends Fragment { ArrayAdapter<String> mConferenceAdapter; public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Create some dummy data for the ListView. Here's a sample weekly forecast String[] teams = getResources().getStringArray(R.array.westTeams); List<String> conferenceList = new ArrayList<String>(Arrays.asList(teams)); // Now that we have some dummy forecast data, create an ArrayAdapter. // The ArrayAdapter will take data from a source (like our dummy forecast) and // use it to populate the ListView it's attached to. mConferenceAdapter = new ArrayAdapter<String>( getActivity(), // The current context (this activity) R.layout.list_item_conferences, // The name of the layout ID. R.id.list_item_conference_textview, // The ID of the textview to populate. conferenceList); View rootView = inflater.inflate(R.layout.fragment_main, container, false); // Get a reference to the ListView, and attach this adapter to it. ListView listView = (ListView) rootView.findViewById(R.id.listview_conferences); listView.setAdapter(mConferenceAdapter); return rootView; } }

How can I make it to where if I click West, it loads those teams, and if I click East, it loads the eastern teams?

--------------Solutions-------------

Your problem is at onItemClick - no matter what you choose, you add WesternConference.class to your intent.
You have to do something like this -

String forecast = mConferenceAdapter.getItem(position);
if (position == 0) {
Intent intent = new Intent(getActivity(), WesternConference.class)
} else {
Intent intent = new Intent(getActivity(), EasternConference.class)
}
intent.putExtra(Intent.EXTRA_TEXT, forecast);
startActivity(intent);

Category:android Time:2018-11-25 Views:3

Related post

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 2.303 (s). 14 q(s)