ListView数据未显示在片段中

时间:2013-05-23 15:08:01

标签: android listview android-fragments android-arrayadapter

这里是产生意外结果的代码。如果有人能指导我做我在这里做错了什么

 public static class LaunchpadSectionFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false);

        ListView moviesList= (ListView) rootView.findViewById(R.id.movies_list);

        ArrayAdapter<String> moviesAdapter;
        List<String> Testing = new ArrayList<String>();
        Testing.add("Hey");
        Testing.add("Hey");
        Testing.add("Hey");
        Testing.add("Hey");
        moviesAdapter= new MyAdapter(getActivity(),android.R.layout.simple_list_item_1,Testing);
        moviesList.setAdapter(moviesAdapter);


return rootView;
}

1 个答案:

答案 0 :(得分:11)

好好玩了很多文章后,我终于找到了解决方法。 这就是我所做的

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

            super.onActivityCreated(savedInstanceState);

            Log.d("CreatedActivity", "LaunchpadFragment");
            ListView moviesList= (ListView) getActivity().findViewById(R.id.movies_list);
            // Gets the ListView from the View list of the parent activity
            // Gets a CursorAdapter

            ArrayAdapter<String> moviesAdapter;
            List<String> Testing = new ArrayList<String>();
            Testing.add("Hey");
            Testing.add("Hey Do");
            Testing.add("Hey It");
            Testing.add("Hey Please");
            moviesAdapter= new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, Testing);
            moviesList.setAdapter(moviesAdapter);
}

请注意这里的覆盖方法,它是

  

onActivityCreated

Android文档说明

  

如果您对片段的视图是由数据适配器填充的列表视图,则应覆盖onActivityCreated方法并在那里进行填充。

我没有这个blockquote的文档链接:|