Tag Archives: Java

Creating a custom CursorAdapter for Android

I’ve been writing a bit more Android code, and came upon the need to write a custom CursorAdapter for a ListView, as the way I wanted to display data from a Cursor was dependent on relationships between different fields of the database.

Of the problems I encountered, the most obvious was when I tried to inflate views using View.inflate(), and found I would get the following error:

UnsupportedOperationException: addView(View, LayoutParams)
is not supported in AdapterView at android.widget.AdapterView

After a bit of rummaging around, I found that the following bit of code would work when attached to a ListView using setAdapter().

public class ExampleCursorAdapter extends CursorAdapter {
	public ExampleCursorAdapter(Context context, Cursor c) {
		super(context, c);
	}
 
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		TextView summary = (TextView)view.findViewById(R.id.summary);
		summary.setText(cursor.getString(
				cursor.getColumnIndex(ExampleDB.KEY_EXAMPLE_SUMMARY)));
	}
 
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.item, parent, false);
		bindView(v, context, cursor);
		return v;
	}
}

The above example code doesn’t actually require a CursorAdapter, it would easily be implemented using a SimpleCursorAdapter, but it serves to show the idea. Basicly, get a LayoutInflater from the context, and use the version of inflate() that does not attach to the root view.

Writing Parcelable classes for Android

I’ve just released version 0.3 of Xydroid, my small Xymon monitoring app. It should now be a bit more suited to Android 2.0, and have just a couple fewer bugs in it.

As it turns out, the main problem with running under Android 2.0 for my app was de-parceling of a Parcelable representation of the XML files used by Xydroid. Up until now, I had just written out all the data in the tree when parceling, and read until no data was left when de-parceling. As it turns out, if this parcelable class is put into a bundle with other data, such as strings or integers, the de-parceling code will just continue reading along the bundle. The error I tended to encounter would be “Unmarshalling unknown type code 6946932” (or some other number), presumably as I tried to read a String from where an integer existed or similar.

The solution seems to be to write how much data you are planning to parcel as the first bit of information, and only read back this many chunks before passing control back. This works both in 2.0 and 1.5/1.6, whereas reading until there was no more data seemed to work in 1.5/1.6 but not 2.0 — though this might just have been pure luck.

Update: I thought I had better actually show how I ended up implementing my Parcelable classes, so here is a quick example of how to wrap a HashMap in something parcelable:
Continue reading

Xydroid – Xymon monitoring on Android phones

Finally, an update for the blog! Well, what has been going on with me? I’ve been busy at work, and perhaps even busier with my studies. I’m currently doing a course on Concurrency, which got me started on a bit of lovely threaded java programming again. It’s nice to be back to writing a bit of code, and boy have things gotten better over the few years since I last did anything major with it.

In other news, a couple of friends have bought themselves new Android phones, the HTC Hero. More and more of them are coming now, and it really looks like a wagon I’m liable to jump on any day now. They just need to release a new revision of the hardware, preferably something with one of the new Snapdragon CPUs in it …

So what could be more logical than to do a bit of programming for the Android platform? I struggled for a short while to figure out anything worthwhile to write, but then I came up with this: How nice would it be to get the status of your monitored systems direct on your phone? No more needing to browse to the hobbit Xymon system now, just a look at the phone to see how everything you care about is doing. Well, in theory. Except I don’t have the phone yet. Slight flaw in the plan.

None the less, I’ve made the software, in a sort of early beta version, and it’s available on a webpage of its own: Xydroid. Feel free to have a look .. if you want to actually use it, you should probably get in touch, and I’ll be sure to work on it enough that it will actually work properly as well.

After I’d started writing the software – actually, after I’d gotten quite far in the process – I found out that other’s have actually done the same as well, in the form of Xymon Monitor, for which they charge €2.99. Well, at least this might end up as the slightly cheaper alternative 🙂