gpt4 book ai didi

java - ListFragment如何获取listView?

转载 作者:太空狗 更新时间:2023-10-29 23:00:34 24 4
gpt4 key购买 nike

我正在将我的应用程序从 AsyncTasks 移植到 Fragements。

但是我如何访问 fragment 中的 listView (id: list) 元素?

class MyFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.list_fragment, container, false);
ListView listView = getListView(); //EX:
listView.setTextFilterEnabled(true);
registerForContextMenu(listView);
return v;
}
}

xml:

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

例如:

Caused by: java.lang.IllegalStateException: Content view not yet created

最佳答案

作为onCreateView文档保持:

creates and returns the view hierarchy associated with the fragment

因此,由于该方法不返回,您将无法访问 ListView通过getListView() .您可以在 onActivityCreated 中获得有效引用打回来。或者您可以尝试使用 v.findViewById(android.R.id.list)如果ListViewlist_fragment.xml 内声明

关于java - ListFragment如何获取listView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10664940/

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