问题
I'm writing a custom ContentProvider that serves up content consisting of a single, constant string which I represent as a one-row table having columns _id = 0 and value = "SomeString". This string is not stored in a database, so I developed a subclass of CrossProcessCursor that has does everything required to behave like what I described above.
The documentation for CrossProcessCursor is very sparse and doesn't really explain what the fillWindow() method should be doing beyond the obvious. Based on the descriptions of CursorWindow's methods, I put the following together, which I thought should cover it:
public class MyCursor implements CrossProcessCursor {
...
public void fillWindow(int pos, CursorWindow window) {
if (pos != 0) { // There's only one row.
return;
}
window.clear();
window.allocRow(); // TODO: Error check, false = no memory
window.setNumColumns(2);
window.setStartPosition(0);
window.putLong(0, 0, 0);
window.putString("SomeString", 0, 1);
}
}
As expected, it gets called with pos = 0 when a client application requests the content, but the client application throws an exception when it tries to go after the first (and only) row:
Caused by: java.lang.IllegalStateException: UNKNOWN type 48
at android.database.CursorWindow.getLong_native(Native Method)
at android.database.CursorWindow.getLong(CursorWindow.java:380)
at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:108)
at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:194)
at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:248)
at android.database.CursorWrapper.moveToFirst(CursorWrapper.java:86)
...(Snipped)...
Could anyone shed some light on what this method should be doing to return a correct-looking row to the client?
Thanks.
回答1:
For what you're doing you should check out the MatrixCursor. It uses the AbstractCursor#fillWindow
implementation which calls toString
on every object. Since you're just sending a string anyway it should work fine for you.
来源:https://stackoverflow.com/questions/3340552/what-to-do-in-custom-contentproviders-fillwindow-method