My pet programming project of the moment is a tally counter. Not the sort where you can only count one type of thing at a time and can't remember what you were counting and when you last updated it. But one where you can have multiple counters on the go at the same time, each with a meaningful description and, eventually, a timestamp indicating when it was last updated.
Ok. So you're probably asking why I didn't download one from the Android Market... right?
The answer to that is that making a tally counter was one of my original choices for my final assignment for Mobile Applications Development. But it got vetoed in favour of another slightly more complex one. And I wasn't going to start serious work on the tally counter now if I was going to make it for said assignment...
So I got to the point where I was able to add items dynamically to the ListView and each individual counter was incrementing and decrementing perfectly (verified through copious amounts of logging statements). The only thing that I wanted it to do, but it wasn't doing, was to show the ContextMenu when I long-pressed an item in the list.
My first port of call, as with previous programming problems, was 'all-knowing' Google. After trying several approaches to registering the ContextMenu for the ListView and then inflating the ContextMenu, I was stumped. For anyone who is interested, below is a compilation of the approaches that I tried.
Registering the context menu
registerForContextMenu(getListView());
---------------------------------------
ListView list = (ListView)findViewById(android.R.id.list);
registerForContextMenu(list);
Creating the ContextMenu
menu.add(groupId,itemId,order,title);
---------------------------------------
MenuInflater inflater = getMenuInflater();
inflater.inflate(menuId,menu);
No comments:
Post a Comment