ListFragment如何让ListView的? [英] ListFragment how to get the listView?

查看:122
本文介绍了ListFragment如何让ListView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我移植应用程序我从AsyncTasks到Fragements。

但我怎么能访问ListView的?我的片段中(ID列表)元素

 类MyFragment扩展ListFragment {
    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
        视图V = inflater.inflate(R.layout.list_fragment,集装箱,FALSE);
        ListView控件的ListView = getListView(); // EX:
        listView.setTextFilterEnabled(真);
        registerForContextMenu(ListView控件);
        返回伏;
    }
}

XML:

 < ListView控件
        机器人:ID =@机器人:ID /列表
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent>
    < /&的ListView GT;

例如:

 产生的原因:java.lang.IllegalStateException:内容视图尚未创建


解决方案

onCreateView DOC留下:

 创建并返回与片段相关联的视图层次

所以,因为方法不返回,您将无法访问的ListView getListView() 。您可以获取在 onActivityCreated 回调的有效参考。
或者你可以尝试使用 v.findViewById(android.R.id.list)如果的ListView 在里面<声明code> list_fragment.xml

I'm porting my app from AsyncTasks to Fragements.

But how can I access the listView (id: list) element within my fragment?

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>

Ex:

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

解决方案

as the onCreateView doc stays:

creates and returns the view hierarchy associated with the fragment

so since the method does not return, you will are not able to access the ListView through getListView(). You can obtain a valid reference in the onActivityCreated callback. Or you can try using v.findViewById(android.R.id.list) if the ListView is declared inside list_fragment.xml

这篇关于ListFragment如何让ListView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆