gpt4 book ai didi

android - ListView 数据未显示在 fragment 中

转载 作者:太空狗 更新时间:2023-10-29 15:36:37 27 4
gpt4 key购买 nike

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

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

最佳答案

好的,在大量阅读文档之后,我终于找到了一个解决方法。这是我做的

@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 文档状态

If your view of the fragment is a listview that is being filled by a data adapter, you should override the onActivityCreated method and do the filling in there.

虽然我没有指向此 block 引用的文档链接:|

关于android - ListView 数据未显示在 fragment 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16717547/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com