Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger onListItemClick in a ListFragment

In the tablet layout of my app, I have three ListFragments and one regular Fragment, let's call them Make, Model, Size, and Details. Initially the Make list is populated, then based on the Make selection, the Model list is populated; when a Model is selected, the Size list is populated; when a Size is selected, Details are shown. Each of these events (list item selection) is handled through the onListItemClick handler.

On startup, I want to populate the Make list, select the first Make in the list and have it go through the onListItemClick handler to populate the Model list (and so on so that all lists are populated and the details are shown - this should also be the behavior when any selection is made in any of the lists - select the first item in the next list until we get to showing the details). Note that I have control of the DB and for every Make there will always be at least one Model, for each Make/Model at least one Size, for each Make/Model/Size exactly one Detail.

So, I want to select the first item in the list and have it trigger the onListItemClick handler. I've tried the following (with appropriate bounds checking, etc), but it doesn't work.

getListView().setItemChecked(0, true);

The only changes to an "out of the box" ListFragment are to set the CacheColorHint as such

getListView().setCacheColorHint(R.color.GhostWhite);

where GhostWhite is set in the styles.xml as

<color name="GhostWhite">#88FFFFFF</color>

Any ideas?

Thanks in advance.

like image 684
Ed Sinek Avatar asked Dec 23 '11 02:12

Ed Sinek


1 Answers

To select the first item in the list, first get focus from touch, then select the first item, then trigger the onclick handler

int position = 0;
getListView().requestFocusFromTouch();
getListView().setSelection(position);
getListView().performItemClick(getListView().getAdapter().getView(position, null, null), position, position);
like image 81
Ed Sinek Avatar answered Sep 27 '22 21:09

Ed Sinek