سؤال

I have a ListFragment which contains a list however onListItemClick is never called. I am not using getListView() which I suspect is the problem. I am pulling my list view from the xml as such:

    list = (ListView) getActivity().findViewById(android.R.id.list);

And then setting the adapter like this:

    list.setAdapter(new CustomAdapter(getActivity(), R.layout.title, mCursor, new String[]{"title"},  new int[]{R.id.my_title}) );

Since I need to set the adapter on the list, I do not use setListAdapter() either. Is it not possible to pull the list from the xml and use onListItemClick? I would like to keep my list view in the xml so I do not have to set all of the properties programmatically.

If this is not possible, how can I select items in my list?

Thanks

هل كانت مفيدة؟

المحلول

OK. Try this answer. Keep your layout file as it is. Override the following method in your FragmentList class:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    return inflater.inflate(R.layout.your_fragment_layout, container, false);
}

Make sure that your layout define a ListView with the right id, as follows:

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

Then, just call FragmentList::setListAdapter:

setListAdapter(new CustomAdapter(getActivity(), R.layout.title, mCursor, new String[]{"title"},  new int[]{R.id.my_title}) );

Should work, ticky-boo.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top