怎么办在定制的ContentProvider的fillWindow()方法呢? [英] What to do in custom ContentProvider's fillWindow() method?
问题描述
我在写,可供由一个单一的,常量字符串我重新present作为一个行的表有列_id = 0和值=SomeString内容定制的ContentProvider。该字符串不是存储在数据库中,所以我开发CrossProcessCursor的子类,它具有所需的一切表现得像什么上面描述我。
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.
有关CrossProcessCursor文档是很稀疏,并没有真正说明什么fillWindow()方法应该做的不止是显而易见的。基于对CursorWindow的方法的描述,我把下面一起来,我认为应该包括它:
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);
}
}
正如预期的那样,它被称为与POS = 0时,客户端应用程序请求的内容,但是,当它试图第一个(也是唯一一个)一行之后去客户端应用程序抛出异常:
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?
感谢。
推荐答案
有关你在做什么,你应该检查出的 MatrixCursor
。它使用它调用的toString
中的每个对象上的 AbstractCursor#fillWindow
的实施。因为你只是发送一个字符串,无论如何,它应该正常工作适合你。
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.
这篇关于怎么办在定制的ContentProvider的fillWindow()方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!