Is it OK to have one instance of SQLiteOpenHelper shared by all Activities in an Android application?

Click here to see my blog post on this subject. CommonsWare is right on (as usual). Expanding on his post, here is some sample code that illustrates three possible approaches. These will allow access to the database throughout the application. Approach #1: subclassing `Application` If you know your application won’t be very complicated (i.e. if … Read more

Images in SimpleCursorAdapter

I extended SimpleCursorAdapter, and while I did not use a ViewBinder here is my code for using an image stored as a blob in an sqlite database in a listview. This was adapted from an article I read here. My layout file for a row is: row_layout_two_line.xml <?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”match_parent” android:layout_height=”match_parent” android:background=”@drawable/select_item”> … Read more

How to text filter an Android ListView backed by a SimpleCursorAdapter?

For a SimpleCursorAdapter cursor, you only need to use the setFilterQueryProvider, to run another query for your cursor, based on the constraint: m_Adapter.setFilterQueryProvider(new FilterQueryProvider() { public Cursor runQuery(CharSequence constraint) { Log.d(LOG_TAG, “runQuery constraint:”+constraint); //uri, projection, and sortOrder might be the same as previous //but you might want a new selection, based on your filter content … Read more

Changing values from Cursor using SimpleCursorAdapter

The simplest way to format a cursor value is to use SimpleCursorAdapter.setViewBinder(..): SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list, cursor, new String[] { Definition.Item.TITLE, Definition.Item.CREATE_DATE }, new int[] { R.id.title, R.id.createDate}); adapter.setViewBinder(new ViewBinder() { public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) { if (aColumnIndex == 2) { String createDate = aCursor.getString(aColumnIndex); TextView textView = (TextView) … Read more

App Crashes On Startup Due To java.lang.IllegalArgumentException: column ‘_id’ does not exist

I had a similar issue – I think it’s a case of having to ‘select’ (or ‘select as’) something called _id because the SimpleCursorAdapter needs it. From the documentation: Handling content URI IDs By convention, providers offer access to a single row in a table by accepting a content URI with an ID value for … Read more