brain food, good buy, futzing with Gallery

Found Beer Planet yesterday morning, now I have a lot more reading to do.

I bought this Compaq nx 7400 laptop from Craigslist earlier this month just so I’d have something to take to meetups and use the Android SDK on. Didn’t realize it would become my favorite device in the house – seems I’m valuing flexibility and comfort over even the allure of dual monitors. Anyway, a damn good investment at $150.

I thought I’d try creating a little app that displays/demonstrates all of the android.R resources – I mean how do you know what an icon or layout will look like until you try it in an app? So, starting with a gallery of android.R.drawable – should be pretty easy, right? Hahahah. Funny, not a lot of info on the dev guide about Gallery, and following the examples got me a big fat ClassCastException about Gallery$LayoutParams. Geez I hate that thing. There’s no way to relate it back to what I actually did wrong. *sigh*

BTW I created a custom adapter (just extend BaseAdapter) so I could use reflection on android.R.drawable and turn all of the fields into items for the gallery (field value for image resource ID and field name for caption) – that way I don’t need to make a long list and any variation between platforms is captured automatically. I thought that was pretty clever and it’s really not very hard, except for this business of filling in the view.

An initial glance at Gallery reference doc indicates maybe I should try using a simple ImageView as the gallery item just to get going – the Gallery.LayoutParams need to be applied to the Gallery item, but currently my item is actually a LinearLayout holding the image and a caption TextView. That seems to “fix” the ClassCast problem but I’m only seeing one item, no scrolling. How does this Gallery thing work? Time to google… oh wait, there is a Gallery tutorial in the dev guide after all. Durrr. But! I try to use this code:

TypedArray a = obtainStyledAttributes(android.R.styleable.Theme);
mGalleryItemBackground = a.getResourceId(
        android.R.styleable.Theme_galleryItemBackground, 0);
a.recycle();

… and now Eclipse tells me there’s no such thing as android.R.styleable – example fail! WTF? NOW it’s time to google. That doesn’t help; looking at the APIDemos examples, I notice that for 1.5 it used its own R.styleable and not android.R.styleable, so maybe the latter didn’t come into existence until a later API level. The class reference helpfully doesn’t say. And I need to stop here for today :-(

Advertisements

Saving and displaying dates in a list with Android

Trying to clean up some bits that I’ve left sort of hazy. Now it’s time to wrestle with – how do I really get working dates between Java and sqlite and the user? Really two issues: how do I store it in the DB, and how do I get it from the DB and display to the user?

This is always a tad tricky with any DB. In some ways it seems sqlite treats dates like strings; and you can just stuff anything in that column, without it being valid in any sense, and sqlite accepts it without complaint. But it’s useful to notice that if you insert (or select) datetime() it puts it in the useful “YYYY-MM-DD HH:MM:SS” format (useful because at least they compare/sort properly). So I can format times that way to store them – only issue is GMT vs local time.

For displaying to the user, Android has some time formatting functions, but I’m having a heck of a time figuring them out, not being used to the Java idiom I guess. Looks like this blog post has some good pointers. The other tricky point is that I’m currently just handing off the database cursor to be displayed in a ListItem – not sure how to reformat things in between. I tried creating a custom view extending TextView just for the log output (consulting docs here). I’m not sure where I went wrong, but Android simply did not want to use that view; the view builder in ADT had an error about <init> not being defined, and I don’t remember what error I got when trying to run the app. So I asked on IRC and was advised to extend the Adapter instead. Here’s what I ended up with:

public static final String db_DATE_FORMAT = “yyyy-MM-dd HH:mm:ss”;
public static final DateFormat dbDateFormat = new SimpleDateFormat(db_DATE_FORMAT);

@Override
public void setViewText(TextView v, String text) {
if(v.getId() == R.id.ilr_itemLog && text != null && text.length() > 0) {
// try to reformat date text as a local date
try {
Date d = C.dbDateFormat.parse(text.toString());
text = d.toLocaleString();
} catch (ParseException e) {
Log.d(TAG, “Date parsing failed for ” + text);
}
}
super.setViewText(v, text);
}

This seems to work fine. But the real test is later when I let the user modify the log time; we’ll see.