如何使用sqlite视图更新Loader?

时间:2013-12-09 12:44:39

标签: android sqlite android-sqlite android-loader

我遇到了 LoaderManager 的问题。我在sqlite数据库中有一些表,并且还有表示视图,它从表中获取数据。

我也实现了LoaderManager,就像在that excellent guide中一样,它非常适合表格。

但我想更新一个表,并且不是从它获得更新结果,而是从sqlite视图获取更新表。在这种情况下,LoaderManager似乎无法正常工作(onLoadFinished回调不会被触发)

我更新的表架构:

 CREATE TABLE [table_scan] (
   [_id] INTEGER PRIMARY KEY AUTOINCREMENT,
   [NR_ID] INTEGER NOT NULL,
   [T_ID] INTEGER NOT NULL,
   [Color_ID] INTEGER NOT NULL,
   [R_ID] INTEGER NOT NULL,
   [Barcode] TEXT NOT NULL,
   [NumberSeat] INTEGER,
   [Date] DATETIME NOT NULL DEFAULT(DATETIME('now', 'localtime')),
   [Deleted] INTEGER NOT NULL DEFAULT '0',
   [Status] INTEGER NOT NULL DEFAULT '0',
   [Export] INTEGER NOT NULL DEFAULT '0');

我的sqlite视图:

CREATE VIEW [view_scan] AS SELECT _id, Barcode, Status, Deleted, NumberSeat,
 goods_catalog.T_Articul, colors_catalog.Color_Name, sizes_catalog.R_Name
 FROM table_scan
   INNER JOIN goods_catalog ON goods_catalog.T_ID = table_scan.T_ID
   INNER JOIN colors_catalog ON colors_catalog.Color_ID = table_scan.Color_ID
   INNER JOIN sizes_catalog ON sizes_catalog.R_ID = table_scan.R_ID
 WHERE Deleted = 0;

2 个答案:

答案 0 :(得分:1)

下面这么做很容易,不需要使用触发器!

public Cursor query(...) {
    ...
    case view_scan
    ...

    cursor.setNotificationUri(resolver, AUTHORITY_URI);
}
public Uri insert(Uri uri, ContentValues values) {
    ...
    case table_scan:
    ...
    cursor.setNotificationUri(resolver, AUTHORITY_URI);
}

答案 1 :(得分:0)

找到了解决方法。不是很干净,但有效:

Uris for LoaderManager init和更新数据必须等于。 所以,我们需要更新视图。我们可以为此创建触发器。

首先,修改视图以获取所有需要的列:

CREATE VIEW [view_scan] AS SELECT
  table_scan._id, table_scan.NR_ID, 
  table_scan.T_ID,table_scan.Color_ID,
  table_scan.R_ID, table_scan.Barcode,
  table_scan.NumberSeat, table_scan.Deleted, 
  table_scan.Status,
  goods_catalog.T_Articul,
  colors_catalog.Color_Name,
  sizes_catalog.R_Name
FROM table_scan
 INNER JOIN goods_catalog ON goods_catalog.T_ID = table_scan.T_ID
 INNER JOIN colors_catalog ON colors_catalog.Color_ID = table_scan.Color_ID
 INNER JOIN sizes_catalog ON sizes_catalog.R_ID = table_scan.R_ID
WHERE Deleted = 0;

接下来,为view_scan创建触发器:

CREATE TRIGGER insert_view_scan
  instead of insert on view_scan 
   begin
    insert into table_scan(NR_ID,T_ID,Color_ID,R_ID,Barcode,NumberSeat,Status)
    values(new.NR_ID, new.T_ID, new.Color_ID, new.R_ID, new.Barcode, new.NumberSeat, new.Status);
   end;

现在,我们可以将数据更新为 view_scan 而不是 table_scan ,并且LoaderManager正常运行。