带有ListView的片段不会使用Arrayadapter显示任何内容

时间:2015-01-22 08:16:20

标签: android android-fragments android-listview

我正在尝试使用ArrayAdapter填充包含ListView的Fragment,但是一旦我运行应用程序,ListView就完全空白了。我做错了什么?

这是我的fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.medeasynetwork.MainActivity$PlaceholderFragment">

<ListView 
    android:id="@+id/myInfoLV"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>

</RelativeLayout>

这里也是我的MainActivity类中的PlaceholderFragment类:

public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */

    private static int selection;
    private static int fragmentId;

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        selection = sectionNumber;
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = null;
        ListView myInfoLV;

        switch (selection) {
        case 1:
            rootView = inflater.inflate(R.layout.fragment_main, container,                                                                             false);
            myInfoLV = (ListView) rootView.findViewById(R.id.myInfoLV);

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, R.array.myInfoArray);
            myInfoLV.setAdapter(adapter);
            break;

        case 2:
            rootView = inflater.inflate(R.layout.fragment_my_directory, container, false);
            break;

        }

        return rootView;

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

案例2工作正常,因为我在XML中填充了ListView,但是当我尝试使用ArrayAdapter时,ListView显示为空白。请帮我弄清问题是什么。

提前致谢!

0 个答案:

没有答案