Monday, April 2, 2012

Android ListViews in Tablet Fragments

Putting together a ListView demo for my mobile programming class turned out to be a bit of a pain. I guess I set myself for difficulty by trying to replicate Paul Hegarty's demo of adding a favourites list to a graphing tablet calculator, but I thought I had sufficiently reduced the difficulty level by not trying to replicate the iPad popover, and instead went for a simple ListView embedded in the main Calculator view.

I fell foul of a number of issues, several of which I have encountered before and one might have hoped I wouldn't have been tripped by them again.  They include

1) need for embedded ListViews to have special IDs
2) nature of ArrayAdapters
3) variations arising from fragments

One of the early errors that slowed me down and forced me to create a separate FavesFragment to handle the list in isolation was the "inflating fragment error"  The following StackOverflow posts helped orient me ...

I spent a lot of time thinking that an AsyncTask was going to be necessary in order to enable the listview data to be updated (e.g. add more rows), but that turned out not to be the case.  The key trick there was to do any addition to the data through ArrayAdapter.add, and basically forget about any reference to the initial data passed to ArrayAdapter - ultimately I just initialized with an empty ArrayList.  There were several confusing StackOverflow posts that deflected me from ultimately finding salvation in the android irc channel on

Well this second one should have helped me more, but I was trying to do much at once, as well as programming Pythonically in Java, which often gets me into trouble ...

And I think this android developer blog got me a bit further:

but annoyingly it was like much of the android docs; code fragments, but not an actual working system.  Ultimately it was IRC that sorted me out, with other developers pointing out that the key issue was updating the data through the ArrayAdapter itself.

I also burnt time seeing if I could get some simple persistence going with Preferences, but it seemed that unlike in iOS, I couldn't store any more than a simple set of Strings in light weight preferences, which wasn't quite sufficient for the stack object I was working with, or maybe it would have been okay?  Hmm ....

No comments: