Question

J'ai un ListFragment qui contient une liste mais onListItemClick est jamais appelée. Je n'utilise pas getListView () que je pense est le problème. Je tirais mon avis la liste du xml en tant que tel:

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

Et puis en définissant l'adaptateur comme ceci:

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

Depuis que je dois mettre l'adaptateur sur la liste, je ne l'utilise setListAdapter () non plus. Est-il pas possible de tirer la liste à partir du XML et utiliser onListItemClick? Je voudrais garder mon point de vue de la liste dans le fichier XML, donc je n'ai pas définir toutes les propriétés d'un programme.

Si cela est impossible, comment puis-je sélectionner des éléments dans ma liste?

Merci

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top