Note that there are some explanatory texts on larger screens.

plurals
  1. POCursor.getType() for API Level <11
    text
    copied!<p>I'm querying the CallLog content provider and need to detect the column types.</p> <p>In Honeycomb and newer (API Level 11+) you can get a columns preferred data type by calling the method <code>Cursor.getType(int columnIndex)</code> which returns one of the following types:</p> <ul> <li>FIELD_TYPE_NULL (0)</li> <li>FIELD_TYPE_INTEGER (1)</li> <li>FIELD_TYPE_FLOAT (2)</li> <li>FIELD_TYPE_STRING (3)</li> <li>FIELD_TYPE_BLOB (4)</li> </ul> <p>How can I accomplish this on pre-Honeycomb &lt;11 devices?</p> <p>I've tried the following:</p> <pre><code>for ( int i = 0; i &lt; cursor.getColumnCount(); i++ ) { int columnType = -1; try { cursor.getInt( i ); columnType = Cursor.FIELD_TYPE_INTEGER; } catch ( Exception ignore ) { try { cursor.getString( i ); columnType = Cursor.FIELD_TYPE_STRING; } catch ( Exception ignore1 ) { try { cursor.getFloat( i ); columnType = Cursor.FIELD_TYPE_FLOAT; } catch ( Exception ignore2 ) { try { cursor.getBlob( i ); columnType = Cursor.FIELD_TYPE_BLOB; } catch ( Exception ignore3 ) { columnType = Cursor.FIELD_TYPE_NULL; } } } } } </code></pre> <p>However, no exception is thrown. The data is always casted in the first type you are checking for, in this case getInt(). That means, I get the correct values if the column type is <strong>Integer</strong> but a <strong>0</strong> for all other types.</p> <p>Why am I not looking in the documentation to check what type is stored? The columns differ depending on the device manufacturer and not all of them are documented, see this question: <a href="https://stackoverflow.com/questions/11504654/how-to-handle-manufacturer-dependent-differences-in-contentproviders">How to handle manufacturer-dependent differences in ContentProviders?</a></p> <p>Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload