You are currently viewing the BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
With the ToDo List example, I would like to be able to set the focus to a particular item in the list, by code. For example I want to set the first item as highlighted automatically at the beginning, under program control. I have tried everything I can think off but not successful yet. Can anyone help please?
Further to my problem, I have successfully used
" myListView.setSelection0); " to position the place in the array list correctly.
That is part of my problem solved. The remaining part of the problem is that I have been unable to turn on the highlight at this position.
When I press the down key on the dpad the highlight turns on at the position I have set as outlined above. But when I try to use
" myListView.getSelectedItemPosition() " without manually highlighting the position first, the app crashes. That is why I want to highlight
an item by program control, to prevent such a crash. Thanks for your help so far. Is there any way to turn on the selection highlight?
Sorry, but I'm still not clear on the problem you're having. My ToDo app starts up fine with the second (or any) item selected and highlighted.
I don't have to press the D-Pad to get the highlight bar to appear. And myListView.getSelectedItemPosition() doesn't cause a crash.
Are you making sure to set the list position after the list is fully populated? I placed this code at the very bottom of the onCreate() routine of ToDoList.java.