如何将子片段链接到MainActivity?

时间:2015-06-10 17:38:26

标签: android android-fragments

我建立了一个非常简单的NBA应用程序,因为我是新手。

MainActivity有一个包含两个项目的ListView。西部队和东部队。当用户点击某个项目时,它会显示该会议的团队。这些团队作为数组项存储在strings.xml文件中。

以下是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;
    }
}

我现在面临的问题是,无论选择哪种选择,它都只显示西方球队。

这是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;
    }
}

如果我点击West,它如何加载那些团队,如果我点击East,它会加载东部团队,我该如何进入?

0 个答案:

没有答案