Android onListItemClick未在ListFragment中调用

Android onListItemClick未在ListFragment中调用,android,listview,fragment,Android,Listview,Fragment,我有一个ListFragment,它包含一个列表,但是从不调用onListItemClick。我没有使用getListView(),我怀疑这就是问题所在。我从xml中提取列表视图,如下所示: list = (ListView) getActivity().findViewById(android.R.id.list); 然后按如下方式设置适配器: list.setAdapter(new CustomAdapter(getActivity(), R.layout.title, m

我有一个ListFragment,它包含一个列表,但是从不调用onListItemClick。我没有使用getListView(),我怀疑这就是问题所在。我从xml中提取列表视图,如下所示:

    list = (ListView) getActivity().findViewById(android.R.id.list);
然后按如下方式设置适配器:

    list.setAdapter(new CustomAdapter(getActivity(), R.layout.title, mCursor, new String[]{"title"},  new int[]{R.id.my_title}) );
因为我需要在列表上设置适配器,所以我也不使用setListAdapter()。是否无法从xml中提取列表并使用onListItemClick?我希望将列表视图保留在xml中,这样就不必以编程方式设置所有属性

如果不可能,如何在列表中选择项目


谢谢

好的。试试这个答案。保持布局文件的原样。在FragmentList类中重写以下方法:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    return inflater.inflate(R.layout.your_fragment_layout, container, false);
}
确保您的布局使用正确的id定义ListView,如下所示:

 <ListView android:id="@id/android:list"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:layout_weight="1"
           android:drawSelectorOnTop="false"/>

应该行的,蒂奇·布。

我不清楚你为什么不使用标准模式。在ListFragment上调用setListAdapter,将其传递给Customadapter。您可以这样做,但仍然可以通过在newView中膨胀列表来定义布局中的列表。您可能通过不调用setListAdapter绕过了一些代码。看起来我无法在ListFragment中重写newView。我可以从我的CustomAdapter中找到它,因为它扩展了SimpleCursorAdapter,但是,我认为这不是适合它的地方。我已经更新了答案。我从错误的地方获取了代码。方法是相同的,但覆盖onCreateView。谢谢。没有onCreateView是导致xml中的列表无法显示的原因。
setListAdapter(new CustomAdapter(getActivity(), R.layout.title, mCursor, new String[]{"title"},  new int[]{R.id.my_title}) );