I would like to write a ContentObserver for my app's local table. Also I have ContentProvider to access the table. Now in one of my Activity i have to observe for a change in only one row of that table. I have the primary key to observe for it but the primary key is a TEXT not INTEGER. When googled on it I found about getting specific row's Uri by using ContentUris.withAppendedId method. But it requires id must be long(NUMBER). So is there a way I can get Uri of single row of a table when the id is TEXT?
From the comments of #pskink I found the way to achieve it and would like to share it. In the existing content provider's uri matcher we have to add '/*' with new entry as follows...
private static final String AUTH = "com.test.Provider";
public static final Uri TABLE_URI = Uri.parse("content://" + AUTH + "/"
+ TABLE_NAME);
public static final Uri TABLE_ID_URI = Uri.parse("content://" + AUTH
+ "/" + TABLE_NAME + "/*");
final static int TABLE_COMMENT = 10;
final static int TABLE_ROW_COMMENT = 11;
uriMatcher.addURI(AUTH, TABLE_NAME, TABLE_COMMENT);
uriMatcher.addURI(AUTH, TABLE_NAME + "/*", TABLE_ROW_COMMENT);
And in the query method as follows...
#Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String orderBy) {
db = dbManager.getReadableDatabase();
Cursor cursor = null;
switch (uriMatcher.match(uri)) {
case TABLE_COMMENT:
cursor = db.query(TABLE_NAME, projection, selection,selectionArgs, null, null, orderBy);
if (cursor != null)
cursor.setNotificationUri(getContext().getContentResolver(),uri);
break;
case TABLE_ROW_COMMENT:
String id = uri.getLastPathSegment();//getting the PK
cursor = db.query(TABLE_NAME, projection,Table._ID + "=?", new String[] { id },null, null, orderBy);
if (cursor != null)
cursor.setNotificationUri(getContext().getContentResolver(),uri);
break;
}
return cursor;
}
And from the activity or fragment can get a specific row's uri from the loader as follows...
Uri uri = Uri.withAppendedPath(Provider.TABLE_URI,
"pk value");
Cursor cursor = getContentResolver().query(uri, null, null, null,
null);
And in my case I'll use this Uri for Observer.
Related
I am trying to write a simple content provider and populate a ListView using these references:
https://developer.android.com/reference/android/app/ListActivity.html
http://www.newthinktank.com/2015/01/make-android-apps-21/
I looked at this thread but it does not seem to be my issue:
SimpleCursorAdapter to populate ListView
The database seems to work, but when I try to bind to my ListView it gives an error of a missing column '_id', but I have it since I can log the contents of the database without problems. Code snippets below:
Logging the database (This WORKS!):
public void logAllPatients() {
// Projection contains the columns we want
String[] projection = new String[]{"id", "name"};
// Pass the URL, projection and I'll cover the other options below
Cursor cursor = resolver.query(CONTENT_URL, projection, null, null, null);
// Cycle through and display every row of data
if (cursor.moveToFirst()) {
do {
String patientList = "";
String id = cursor.getString(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
patientList = patientList + id + " : " + name + "\n";
Log.d(TEST_CONTENT_PROVIDER, patientList);
} while (cursor.moveToNext());
}
}
trying to populate the listview (missing column?, why)?
private void bindAllPatients() {
try {
// Projection contains the columns we want
String[] projection = new String[]{"id", "name"};
Cursor cursor = resolver.query(CONTENT_URL, projection, null, null, null);
if (cursor != null) {
startManagingCursor(cursor);
cursor.moveToFirst();
// Now create a new list adapter bound to the cursor.
// SimpleListAdapter is designed for binding to a Cursor.
ListAdapter adapter = new SimpleCursorAdapter(
this, // Context.
android.R.layout.two_line_list_item,
cursor, // Pass in the cursor to bind to.
new String[]{"id", "name"}, // Array of cursor columns to bind to.
new int[]{R.id.my_id, R.id.my_name}, 0);
// Parallel array of which template objects to bind to those columns.
// Bind to our new adapter.
setListAdapter(adapter);
cursor.close();
}
} catch (Exception e) {
Log.e(TEST_CONTENT_PROVIDER, e.toString());
}
}
The output log:
D/GMO_CONTENT_PROVIDER: 9 : Joe
D/GMO_CONTENT_PROVIDER: 10 : Mary
E/GMO_CONTENT_PROVIDER: java.lang.IllegalArgumentException: column '_id' does not exist
here's the database creation:
private SQLiteDatabase sqlDB;
static final String DATABASE_NAME = "myPatients";
static final String TABLE_NAME = "patients";
static final String CREATE_DB_TABLE = "CREATE TABLE " + TABLE_NAME +
"(id INTEGER PRIMARY KEY AUTOINCREMENT, " + " name TEXT NOT NULL);";
// bunch of code
#Override
public void onCreate(SQLiteDatabase sqlDB) {
try {
sqlDB.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
sqlDB.execSQL(CREATE_DB_TABLE);
} catch (Exception e) {
Log.e(TEST_CONTENT_PROVIDER, e.toString());
}
}
and the query override, which works!
#Nullable
#Override
public Cursor query(#NonNull Uri uri, #Nullable String[] projection, #Nullable String selection, #Nullable String[] selectionArgs, #Nullable String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(TABLE_NAME);
switch (uriMatcher.match(uri)) {
case uriCode:
queryBuilder.setProjectionMap(values);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
Cursor cursor = queryBuilder.query(sqlDB, projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
any help is greatly appreciated!
The correct answer:
Android column '_id' does not exist?
"SimpleCursorAdapter requires that the Cursor's result set must include a column named exactly "_id"."
I'm querying the ContactsContract.Data table to find phone records.
I get an error when I create a new CursorLoader:
java.lang.IllegalArgumentException: Invalid column deleted
My code:
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Data;
...
String[] projection = {
Phone.DELETED,
Phone.LOOKUP_KEY,
Phone.NUMBER,
Phone.TYPE,
Phone.LABEL,
Data.MIMETYPE,
Data.DISPLAY_NAME_PRIMARY
};
// "mimetype = ? AND deleted = ?"
String selection = Data.MIMETYPE + " = ? AND " Phone.DELETED + " = ?";
String[] args = {Phone.CONTENT_ITEM_TYPE, "0"};
return new CursorLoader(
this,
Data.CONTENT_URI,
projection,
selection,
args,
null);
Any idea why the Phone.DELETED column isn't included in the cursor? The documentation does say -
Some columns from the associated raw contact are also available
through an implicit join.
Looks like you've found a feature that has been documented in many places, but hadn't been implemented yet. I opened a bug for tracking this issue - lets see what AOSP guys have to say on the subject (bug report).
Meanwhile, you can use the following workaround:
Uri uri = ContactsContract.RawContactsEntity.CONTENT_URI;
String[] projection = {
Phone._ID,
Phone.DELETED,
//Phone.LOOKUP_KEY,
Phone.NUMBER,
Phone.TYPE,
Phone.LABEL,
Data.MIMETYPE,
Data.DISPLAY_NAME_PRIMARY
};
String selection = Data.MIMETYPE + " = ? AND " + Data.DELETED + " = ?";
String[] args = {
Phone.CONTENT_ITEM_TYPE, "0"
};
return new CursorLoader(
this,
uri,
projection,
selection,
args,
null);
Changes:
Use RawContactsEntity's URI
LOOKUP_KEY is not accessible via above URI - you'll have to execute additional query if you absolutely need this column
_ID column will be required if you are going to use the resulting Cursor in CursorAdapter.
Edit: following #MichaelAlanHuff's request I'm posting the parts of code which this answer is based upon
From com.android.providers.contacts.ContactsProvider2#queryLocal() (source code of ContactsProvider2):
protected Cursor queryLocal(final Uri uri, final String[] projection, String selection,
String[] selectionArgs, String sortOrder, final long directoryId,
final CancellationSignal cancellationSignal) {
final SQLiteDatabase db = mDbHelper.get().getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String groupBy = null;
String having = null;
String limit = getLimit(uri);
boolean snippetDeferred = false;
// The expression used in bundleLetterCountExtras() to get count.
String addressBookIndexerCountExpression = null;
final int match = sUriMatcher.match(uri);
switch (match) {
...
case DATA:
case PROFILE_DATA:
{
final String usageType = uri.getQueryParameter(DataUsageFeedback.USAGE_TYPE);
final int typeInt = getDataUsageFeedbackType(usageType, USAGE_TYPE_ALL);
setTablesAndProjectionMapForData(qb, uri, projection, false, typeInt);
if (uri.getBooleanQueryParameter(Data.VISIBLE_CONTACTS_ONLY, false)) {
qb.appendWhere(" AND " + Data.CONTACT_ID + " in " + Tables.DEFAULT_DIRECTORY);
}
break;
}
...
}
qb.setStrict(true);
// Auto-rewrite SORT_KEY_{PRIMARY, ALTERNATIVE} sort orders.
String localizedSortOrder = getLocalizedSortOrder(sortOrder);
Cursor cursor = query(db, qb, projection, selection, selectionArgs, localizedSortOrder, groupBy,
having, limit, cancellationSignal);
if (readBooleanQueryParameter(uri, Contacts.EXTRA_ADDRESS_BOOK_INDEX, false)) {
bundleFastScrollingIndexExtras(cursor, uri, db, qb, selection,
selectionArgs, sortOrder, addressBookIndexerCountExpression,
cancellationSignal);
}
if (snippetDeferred) {
cursor = addDeferredSnippetingExtra(cursor);
}
return cursor;
}
As you can see, there are two additional methods where SQLiteQueryBuilder used to build the query could be changed: setTablesAndProjectionMapForData() and additional query() method.
Source of com.android.providers.contacts.ContactsProvider2#setTablesAndProjectionMapForData():
private void setTablesAndProjectionMapForData(SQLiteQueryBuilder qb, Uri uri,
String[] projection, boolean distinct, boolean addSipLookupColumns, Integer usageType) {
StringBuilder sb = new StringBuilder();
sb.append(Views.DATA);
sb.append(" data");
appendContactPresenceJoin(sb, projection, RawContacts.CONTACT_ID);
appendContactStatusUpdateJoin(sb, projection, ContactsColumns.LAST_STATUS_UPDATE_ID);
appendDataPresenceJoin(sb, projection, DataColumns.CONCRETE_ID);
appendDataStatusUpdateJoin(sb, projection, DataColumns.CONCRETE_ID);
appendDataUsageStatJoin(
sb, usageType == null ? USAGE_TYPE_ALL : usageType, DataColumns.CONCRETE_ID);
qb.setTables(sb.toString());
boolean useDistinct = distinct || !ContactsDatabaseHelper.isInProjection(
projection, DISTINCT_DATA_PROHIBITING_COLUMNS);
qb.setDistinct(useDistinct);
final ProjectionMap projectionMap;
if (addSipLookupColumns) {
projectionMap =
useDistinct ? sDistinctDataSipLookupProjectionMap : sDataSipLookupProjectionMap;
} else {
projectionMap = useDistinct ? sDistinctDataProjectionMap : sDataProjectionMap;
}
qb.setProjectionMap(projectionMap);
appendAccountIdFromParameter(qb, uri);
}
Here you see the construction of table argument of the final query using StringBuilder which is being passed to several append*() methods. I'm not going to post their source code, but they really join the tables that appear in methods' names. If rawContacts table would be joined in, I'd expect to see a call to something like appendRawContactJoin() here...
For completeness: the other query() method that I mentioned does not modify table argument:
private Cursor query(final SQLiteDatabase db, SQLiteQueryBuilder qb, String[] projection,
String selection, String[] selectionArgs, String sortOrder, String groupBy,
String having, String limit, CancellationSignal cancellationSignal) {
if (projection != null && projection.length == 1
&& BaseColumns._COUNT.equals(projection[0])) {
qb.setProjectionMap(sCountProjectionMap);
}
final Cursor c = qb.query(db, projection, selection, selectionArgs, groupBy, having,
sortOrder, limit, cancellationSignal);
if (c != null) {
c.setNotificationUri(getContext().getContentResolver(), ContactsContract.AUTHORITY_URI);
}
return c;
}
The inspection of the above chain of methods led me to the conclusion that there is an officially documented feature which is not implemented.
I have created a content uri from one app :
content://SendDataProvider/bookevent
i am able to insert the values into it.
like :
content://SendDataProvider/bookevent/1
content://SendDataProvider/bookevent/2 etc...
but when i call it to other app for geting all record from this URI i use
String bookContentDataLog = "content://SendDataProvider/bookevent";
Uri bookrecord = Uri.parse(bookContentDataLog);
Cursor cursor = BookShelfActivity.this.getContentResolver().query(
bookrecord, null, null, null, null);
if (cursor.moveToFirst()) { // I got Null pointer exception here
String sessionId = cursor.getString(cursor.getColumnIndex(BOOK_COLUMN_SESSIONID));
String magId = cursor.getString(cursor.getColumnIndex(BOOK_COLUMN_MAG_ID));
}
I debug it and i found that
BookShelfActivity.this.getContentResolver().query(
bookrecord, null, null, null, null);
this return null value to cursor.
How i get all value inserted to this "content://SendDataProvider/bookevent"?
============== I have Used method to insert to uri ===================
1) Class Name and variables
public class SendDataProvider extends ContentProvider {
public SendDataProvider(Context c) {
this.context = c;//getContext();
BookDb dbHelper = new BookDb(context);
db = dbHelper.getWritableDatabase();
}
2) content provider class methode :===
#Override
public Uri insert(Uri uri, ContentValues contentValues) throws NullPointerException {
Log.e("SendDataProvider #Override inserted call","Uri insert");
long rowID = db.insert(BOOK_TABLE_NAME, null, contentValues);
Log.e("SendDataProvider #Override inserted ID","Uri insert ID"+rowID);
if (rowID > 0) {
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
this.context.getContentResolver().notifyChange(_uri, null);
return _uri;
}
throw new SQLException("Failed to add a record into " + uri);
}
3) query
#Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(BOOK_TABLE_NAME);
switch (uriMatcher.match(uri)) {
case uriCode:
qb.setProjectionMap(this.values);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
if (sortOrder == null || sortOrder == "") {
sortOrder = BOOK_COLUMN_MAG_ID;
}
Cursor c = qb.query(db, projection, selection, selectionArgs, null,
null, sortOrder);
c.setNotificationUri(this.context.getContentResolver(), uri);
return c;
}
SOLVED :
Step 1:
create Provider class with DB inser/update/delte/query and be carefull about this things.
static final String PROVIDER_NAME = "com.example.contentprovideruser.DataProvider";//"org.geometerplus.android.fbreader.interfaces.SendDataProvider";
static final String URL = "content://" + PROVIDER_NAME + "/bookevent";
static final Uri CONTENT_URI = Uri.parse(URL);
You need to create well URI
Step 2: Add PROVIDER_NAME AndroidManifest.xml
<provider android:name=".DataProvider"
android:authorities="com.example.contentprovideruser.DataProvider"
android:exported="true"
android:multiprocess="true">
</provider>
Step 3:Need to specify well MIME TYPE
#Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case BOOK:
return "vnd.android.cursor.dir/vnd.booktype";
case BOOK_ID:
return "vnd.android.cursor.item/vnd.booktype";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
step 4 :"CONTENT_URI" is your global uri where all value are inserted
you can use query to get result.
Uri students = DataProvider.CONTENT_URI;
Cursor c = getContentResolver().query(students, null, null, null, "name");
if (c.moveToFirst()) {
do{
Toast.makeText(this,
c.getString(c.getColumnIndex(DataProvider.BOOK_COLUMN_EVENT_NAME)) +
", " + c.getString(c.getColumnIndex( DataProvider.BOOK_COLUMN_FROMPAGE_NO)),
Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
BE happy :)
I need to read(query) a exact row of a column of my database. This is the relevant data of my provider:
public class TravelOrderProvider extends ContentProvider {
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/travelorder");
private static final int URI_TRAVELORDER = 1;
private static final int URI_TRAVELORDER_ITEM = 2;
private static final UriMatcher mUriMatcher;
static {
mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
mUriMatcher.addURI(AUTHORITY, "travelorder", URI_TRAVELORDER);
mUriMatcher.addURI(AUTHORITY, "travelorder/#", URI_TRAVELORDER_ITEM);
}
public class TravelOrder implements BaseColumns {
public static final String NAME = "name";
public static final String GROUP = "group";
public static final String ORDER = "order";
}
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
database = mDbHelper.getWritableDatabase();
int match = mUriMatcher.match(uri);
SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
qBuilder.setTables(TravelOrderDatabaseHelper.TABLE_NAME);
switch (match){
case URI_TRAVELORDER:
//nothing
break;
case URI_TRAVELORDER_ITEM:
String id = uri.getPathSegments().get(1);
qBuilder.appendWhere(TravelOrder._ID + "=" + id);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
Cursor c = qBuilder.query(database, projection, selection, selectionArgs, null, null, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
Now, this is what I'm trying to do in my Activity:
private static final String[] PROJECTION = {TravelOrder._ID, TravelOrder.NAME, TravelOrder.GROUP, TravelOrder.ORDER};
Cursor c = getContentResolver().query(TravelOrderProvider.CONTENT_URI, PROJECTION, null, null, null);
My question is: How can I get a especific row of the GROUP column? I now that this should be done in the last code line above, but I have tryed defining the column and the row in the selection and selectionArgs definitions without result.
Regarding the question:
How can I get a especific row of the GROUP column?
There is a pretty well explained example on how to "Read Information from a Database" on the Android training pages.
Quoting:
SQLiteDatabase db = mDbHelper.getReadableDatabase();
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {
FeedEntry._ID,
FeedEntry.COLUMN_NAME_TITLE,
FeedEntry.COLUMN_NAME_UPDATED,
...
};
// How you want the results sorted in the resulting Cursor
String sortOrder =
FeedEntry.COLUMN_NAME_UPDATED + " DESC";
Cursor c = db.query(
FeedEntry.TABLE_NAME, // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
So, by altering the selection and selectionArgs parameters accordingly, you can surely get a especific row of the GROUP column of your database.
For example, let's say that you want to get the rows that match an specific value of your GROUP column. So you should set up your query like this:
SQLiteDatabase db = mDbHelper.getReadableDatabase();
/* The columns used for the SELECT statement. */
String[] projection = {
"ID",
"GROUP"
};
/*
* The columns used for the WHERE statement,
* they should be formatted as a prepared statement.
*/
String selection = "`ID` = ? AND `GROUP` = ?";
/*
* The arguments that will be replaced for each ?
* in the above statement.
*/
String[] selectionArgs = {
desiredId, desiredGroup
};
String sortOrder = "`ID` ASC";
Cursor c = db.query(
"MY_TABLE", // The table to query
projection, // The columns to return
selection, // The columns for the WHERE clause
selectionArgs, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
I have looked at this for a couple of days now and I completely can't work out why my content provider return 0 using the arguments I am passing it.
Here's my contentResolver code:
String[] expenditureProjection = {
BusinessOpsDatabase.COL_EXPEND_CAT_ID,
BusinessOpsDatabase.COL_EXPEND_DATE,
BusinessOpsDatabase.COL_EXPEND_AMOUNT,
BusinessOpsDatabase.COL_EXPEND_DESC,
BusinessOpsDatabase.COL_STERLING_EXCHANGE,
BusinessOpsDatabase.COL_COMPANY_ID,
BusinessOpsDatabase.CURRENCY_ID,
BusinessOpsDatabase.COL_MOD_DATE
};
// Defines a string to contain the selection clause
String selectionClause = null;
// An array to contain selection arguments
String[] selectionArgs = {expend_id.trim()};
selectionClause = BusinessOpsExpenditureProvider.EXPENDITURE_ID + "=?";
Log.d(TAG, expend_id+" Selected from list.");
Cursor expendCursor = getContentResolver().query(
BusinessOpsExpenditureProvider.CONTENT_URI, expenditureProjection, selectionClause, selectionArgs, null);
if (null == expendCursor) {
Log.d(TAG, "Expenditure cursor: Is null");
} else if (expendCursor.getCount() < 1) {
Log.d(TAG,"Expenditure cursor: Search was unsuccessful: "+expendCursor.getCount());
} else {
Log.d(TAG,"Expenditure cursor: Contains results");
int i=0;
expendCursor.moveToFirst();
// loop through cursor and populate country array
while (expendCursor.isAfterLast() == false)
{
expend_date_edit.setText(expendCursor.getString(1));
expend_amount_edit.setText(expendCursor.getString(3));
expend_desc_edit.setText(expendCursor.getString(4));
i++;
expendCursor.moveToNext();
}
}
Here's my content provider query method:
#Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = mDB.getWritableDatabase();
// A convenience class to help build the query
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(BusinessOpsDatabase.TABLE_EXPENDITURE);
switch (sURIMatcher.match(uri)) {
case EXPENDITURE:
if(selection != null && selectionArgs != null){
//values.get("company_contact");
String segment = uri.getLastPathSegment();
Log.d(TAG, "Last path segment: "+ segment);
String whereClause = BusinessOpsDatabase.EXPENDITURE_ID + "="+ selectionArgs[0];
Log.d(TAG, "Where clause: "+whereClause);
}
break;
case EXPENDITURE_ID:
// If this is a request for an individual status, limit the result set to that ID
qb.appendWhere(BusinessOpsDatabase.EXPENDITURE_ID + "=" + uri.getLastPathSegment());
break;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
// Query the underlying database
Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, null);
// Notify the context's ContentResolver if the cursor result set changes
c.setNotificationUri(getContext().getContentResolver(), uri);
// Return the cursor to the result set
return c;
}
I'm printing the whereclause to the log and I see '_id=3' which should be fine because I have pulled off a copy of my SQLite database and I can see that the expenditure table has an _id 3 row in it. Any Ideas?
What an epic problem this has been. I found the error in my ContentResolver code.
selectionClause = BusinessOpsExpenditureProvider.EXPENDITURE_ID + "=?";
I was using the EXPENDITURE_ID variable from the provider rather than the database class. The line now reads.
selectionClause = BusinessOpsDatabase.EXPENDITURE_ID + "=?";
And works!