【问题标题】:join android tables and own tables加入 android 表并拥有表
【发布时间】:2014-03-09 08:51:21
【问题描述】:

我有一些桌子:

在安卓中:

Contacts (ContactsContract (android database)): ContactsId, DisplayName, Thumbnail Uri

Phone (android database): ContactsId, Number, Number_Type

在我的应用中:

MyMessageTable (my app database): Number, myMessageId, etc

现在我需要如下一行:

Number, Number_Type, DisplayName , Thumbnail Uri, myMessageId , etc

我想用完光标加载器中的行以显示在 UI 中。 但问题是我无法将我的表与 android 表连接起来。

解决方案:

  1. 单独查询每个表并在上层处理(问题:无法将多游标发送到游标加载器并在 UI 中使用)

  2. Join android table 和 phone table 之后用 MyMessageTable 加入结果(问题:在两个不同的数据库中加入两个不同的表?)

  3. 在 RAM 中缓存联系人和电话表并在上层进行处理,然后将 MyMessageTable 光标发送到 UI。 (问题:Ram 使用和需要时间在 ram 中缓存 android 表)

  4. 将 android 表复制到我的应用程序中的新表并加入 SQLiteQueryBuilder 并使用 ContentObserver 更新我的表。(问题:需要时间来复制和 ContentObserver 在应用程序关闭时不起作用。)

5.使用CursorJoiner。但我不能将多个cursor 推送到UI

你有更好的主意吗?

【问题讨论】:

  • 6 自定义 CursorWrapper 怎么样?
  • @pskink CursorWrapper 只是过滤结果。或者有些事情我不知道......
  • 来自文档“此类的主要用途是扩展游标,同时仅覆盖其方法的子集”,因此您可以通过覆盖几个方法轻松添加一些虚拟列
  • @pskink 我的 UI 需要一个光标加载器。我无法使用光标来制作光标生成器
  • 我知道您需要一个光标加载器,然后创建自定义内容提供程序,在 query() 方法中返回您的光标

标签: android sql join android-contentprovider contacts


【解决方案1】:

创建一些像这样的游标包装器(这是一个穷人的实现,但展示了如何做到这一点):

class CW extends CursorWrapper {
    public CW(Cursor cursor) {
        super(cursor);
    }

    // TODO you will probably need to override these methods:
    // public int getColumnCount()
    // public int getColumnIndex(String columnName)
    // public int getColumnIndexOrThrow(String columnName)
    // public String getColumnName(int columnIndex)
    // public String[] getColumnNames()

    @Override
    public String getString(int columnIndex) {
        if (columnIndex < super.getColumnCount()) {
            return super.getString(columnIndex);
        }
        return "extra column for row #" + getPosition();
    }
}

并测试它:

    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] proj = {
            ContactsContract.Contacts.DISPLAY_NAME
    };
    ContentResolver cr = getContentResolver();
    Cursor c0 = cr.query(uri, proj, null, null, null);
    while (c0.moveToNext()) {
        Log.d(TAG, "onCreate [" + c0.getString(0) + "]");
    }
    c0.close();

    Cursor c1 = new CW(cr.query(uri, proj, null, null, null));
    while (c1.moveToNext()) {
        Log.d(TAG, "onCreate [" + c1.getString(0) + "] [" + c1.getString(1) + "]");
    }
    c1.close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多