在自定义 ContentProvider 的 fillWindow() 方法中做什么?
我正在编写一个自定义 ContentProvider,它提供由单个常量字符串组成的内容,我将其表示为具有列 _id = 0 和 value =“SomeString”的单行表。该字符串不存储在数据库中,因此我开发了 CrossProcessCursor 的子类,它具有像我上面描述的那样的行为所需的一切。
CrossProcessCursor 的文档非常稀疏,并且没有真正解释 fillWindow() 方法应该做什么。根据 CursorWindow 方法的描述,我将以下内容放在一起,我认为应该涵盖它:
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 被调用,但客户端应用程序在尝试执行操作时会抛出异常。在第一行(也是唯一的一行)之后:
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)...
任何人都可以阐明此方法应该做什么来向客户端返回看起来正确的行吗?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您正在做的事情,您应该查看
MatrixCursor
。它使用AbstractCursor#fillWindow
实现,在每个对象上调用toString
。因为无论如何你只是发送一个字符串,所以它应该适合你。For what you're doing you should check out the
MatrixCursor
. It uses theAbstractCursor#fillWindow
implementation which callstoString
on every object. Since you're just sending a string anyway it should work fine for you.