3

Here's the code that's producing unexpected results. If anyone can please guide me to what I'm doing wrong here

 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;
}
4

1 回答 1

12

好的,在浏览了很多文档之后,我终于找到了解决方法。这就是我所做的

@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 方法并在那里进行填充。

不过,我没有指向此块引用的文档链接:|

于 2013-05-24T10:49:49.707 回答