Peter Kevin Reeves wrote: > Are there any good examples on how to write a CursorAdaptor? Has > anyone (outside of Google) successfully written a CursorAdaptor? > > I'm working with a ListActivity and need to map an icon ID in my > SqlLite table to an actual icon image. My CursorAdaptor should take > the icon ID and replace the default icon in the list item XML with the > appropriate icon. > > I've been told that a CursorAdaptor is the best way to do this. That I > can insert myself into the list item drawing process and take > responsibility for changing that one bit of the drawing process. > > If any of you have a good example of how to write a CursorAdaptor, I'd > appreciate it.
By a quick scan of the CursorAdapter docs, it looks like it's only a slight variation on the techniques used to subclass ListAdapter. So, bearing in mind that I haven't tried this personally, here's what I'd do: 1. Create a stub CursorAdapter subclass -- here I'll call it MyCursorAdapter (due to lack of originality at 7:50am). 2. Implement MyCursorAdapter#newView(). This would construct a new View to go into the list, showing the data pointed to by the supplied Cursor. You could use ViewInflate, or manually wire together the View via Java. Regardless, you would also pick out the ImageView for your icon and call setImageResource() based upon your data. 3. Implement MyCursorAdapter#bindView(). This would get called when the adapter wants to re-use an existing View created by newView() but needs to pour in new data (e.g,. the user scrolled the list). Just downcast the supplied View to the appropriate type then update your icon and other data as you did in step #2 above. 4. Pop an instance of MyCursorAdapter into your ListView during onCreate(). The only real difference between this and my prior posts on ArrayAdapter would be that CursorAdapter turns the getView() call into newView() or bindView() calls based on whether there is a View supplied for reuse. Those prior posts can be found at: http://groups.google.com/group/android-beginners/browse_thread/thread/eb3a8c5d1f2de79a/fee3c255a834ac41 http://groups.google.com/group/android-developers/browse_thread/thread/355d3cdfa1c787b8/23fb5117a564103d If this doesn't work, write back with symptoms, stack traces, etc. -- Mark Murphy (a Commons Guy) http://commonsware.com The Busy Coder's Guide to Android Development -- coming in June 2008! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] Announcing the new M5 SDK! http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~----------~----~----~----~------~----~------~--~---

