Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Now if I wanted to query another table, lets say nodo, how would I change the ContentProvider?</p> </blockquote> <p>Querying a new table would mean that you need to add a new <code>Uri</code>, since the <code>Uri</code> selects the datasource, similar to using a different table.</p> <p>You would be adding essentially all the hardcoded values that are already there for the todos for your other table. For example:</p> <pre><code>// ------- usually the same for all private static final String AUTHORITY = "de.vogella.android.todos.contentprovider"; // ------- define some Uris private static final String PATH_TODOS = "todos"; private static final String PATH_REMINDERS = "reminders"; public static final Uri CONTENT_URI_TODOS = Uri.parse("content://" + AUTHORITY + "/" + PATH_TODOS); public static final Uri CONTENT_URI_REMINDERS = Uri.parse("content://" + AUTHORITY + "/" + PATH_REMINDERS); // ------- maybe also define CONTENT_TYPE for each // ------- setup UriMatcher private static final int TODOS = 10; private static final int TODO_ID = 20; private static final int REMINDERS = 30; private static final int REMINDERS_ID = 40; private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH); static { sURIMatcher.addURI(AUTHORITY, PATH_TODOS, TODOS); sURIMatcher.addURI(AUTHORITY, PATH_TODOS + "/#", TODO_ID); sURIMatcher.addURI(AUTHORITY, PATH_REMINDERS, REMINDERS); sURIMatcher.addURI(AUTHORITY, PATH_REMINDERS + "/#", REMINDERS_ID); } //@Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // Using SQLiteQueryBuilder instead of query() method SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); int uriType = sURIMatcher.match(uri); switch (uriType) { case TODO_ID: // Adding the ID to the original query queryBuilder.appendWhere(TodoTable.COLUMN_ID + "=" + uri.getLastPathSegment()); //$FALL-THROUGH$ case TODOS: queryBuilder.setTables(TodoTable.TABLE_TODO); break; case REMINDERS_ID: // Adding the ID to the original query queryBuilder.appendWhere(ReminderTable.COLUMN_ID + "=" + uri.getLastPathSegment()); //$FALL-THROUGH$ case REMINDERS: queryBuilder.setTables(ReminderTable.TABLE_REMINDER); break; default: throw new IllegalArgumentException("Unknown URI: " + uri); } </code></pre> <blockquote> <p>Should I append the table names somehow in queryBuilder.setTables(String inTables)?</p> </blockquote> <p>Yes, if different <code>Uri</code>s read from different tables then set the table based on the Uri match.</p> <blockquote> <p>What about the CONTENT_TYPE and CONTENT_ITEM_TYPE, should there be one for each table?</p> </blockquote> <p>Depends on the actual content type. If they are different and you need a type yes. But you don't need to have them at all. That example defines them but doesn't even use them. It would need to return the type in <code>getType</code>, see <a href="http://developer.android.com/reference/android/content/ContentProvider.html#getType%28android.net.Uri%29">documentation</a>.</p> <blockquote> <p>That about the TODO and TODO_ID varibles and the switch in the query method?</p> </blockquote> <p>Those are constants defined for the <code>UriMatcher</code> which is explained nicely <a href="http://developer.android.com/reference/android/content/UriMatcher.html">here</a>. It's basically a simplification for String matching. A big <code>ContentProvider</code> can have 100 different Uris and selecting the right table in <code>query</code> would be painful if you would have to write <code>if (uri.getPath().equals("todos") { /* code */ } else if (uri..</code> all the way.</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