在自定义 ContentProvider 的 fillWindow() 方法中做什么?

发布于 2024-09-11 08:40:26 字数 1439 浏览 1 评论 0原文

我正在编写一个自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南街女流氓 2024-09-18 08:40:26

对于您正在做的事情,您应该查看 MatrixCursor。它使用 AbstractCursor#fillWindow 实现,在每个对象上调用 toString。因为无论如何你只是发送一个字符串,所以它应该适合你。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文