繁体   English   中英

Android内容提供程序-首先运行“活动启动”屏幕,同时从原始数据填充内容提供程序中的数据库

[英]android content provider - first run Activity Splash screen while filling the database in the content provider from raw data

在我的应用程序中,我有一个内容提供程序,它使用数据库来提供所提供的内容。 首次创建数据库时,需要从Json文件的原始内容填充它。 我的想法是,我在SQLiteOpenHelper子类的onCreate处触发了数据库的填充。 效果很好,但我不确定在应用程序首次运行时如何处理应用程序与内容提供程序之间的通信。 基本上,我想在填充数据库时显示某种启动屏幕。 但是应用程序如何得知

  1. 内容提供商正忙于第一次当填充数据库
  2. 内容提供商已准备就绪

当然,我可以通过使用每个数据集调用内容提供者来填充应用程序中的数据库,但我更喜欢在内容提供者的范围内进行操作,从而使应用程序不必处理json文件等的读取。除了设计偏好之外它还可以使内容提供者更有效地填充数据库,因为它可以一次拥有整个数据集。 我觉得这是不可能的,但我希望我能错过一些简单的要点。

任何建议如何实现这一目标将不胜感激。

谢谢

马丁

使用内容提供程序时,我假设您使用DBHelper类来管理数据库的创建。 以下是android notes示例项目中的代码。

这显示了DBHelper构造函数如何足够智能来确定以前是否已创建数据库。 随后,在createDatabase方法中,我将调用一个方法来从您所说的json文件中预填充数据库。

问题在于,这实际上并不允许您向活动传达尚未初始化数据库的信息。

一种想法可能是您使用SharedPreferences来存储已填充数据库的事实。 然后,您可以在启动时在活动中检查sharedPreference,调用内容提供者以填充数据库,然后将已完成此任务的内容存储在共享首选项中。

请注意,如果您例如从android设置菜单中删除数据,我不确定sharedPreferences是否与数据库保持相同的状态。 您需要检查一下。

http://code.google.com/p/android-notes/source/browse/trunk/src/com/bitsetters/android/notes/DBHelper.java?r=10

public class DBHelper {

private static final String DATABASE_NAME = "notes";
private static final String TABLE_DBVERSION = "dbversion";
private static final String TABLE_NOTES = "notes";
private static final int DATABASE_VERSION = 1;
private static String TAG = "DBHelper";
Context myCtx;

private static final String DBVERSION_CREATE = 
    "create table " + TABLE_DBVERSION + " ("
            + "version integer not null);";

private static final String NOTES_CREATE =
    "create table " + TABLE_NOTES + " ("
        + "id integer primary key autoincrement, "
        + "note text, "
        + "lastedit text);";

private static final String NOTES_DROP =
    "drop table " + TABLE_NOTES + ";";

private SQLiteDatabase db;
/**
 * 
 * @param ctx
 */
public DBHelper(Context ctx) {
    myCtx = ctx;
            try {
                    db = myCtx.openOrCreateDatabase(DATABASE_NAME, 0,null);

                    // Check for the existence of the DBVERSION table
                    // If it doesn't exist than create the overall data,
                    // otherwise double check the version
                    Cursor c =
                            db.query("sqlite_master", new String[] { "name" },
                                            "type='table' and name='"+TABLE_DBVERSION+"'", null, null, null, null);
                    int numRows = c.getCount();
                    if (numRows < 1) {
                            CreateDatabase(db);
                    } else {
                            int version=0;
                            Cursor vc = db.query(true, TABLE_DBVERSION, new String[] {"version"},
                                            null, null, null, null, null,null);
                            if(vc.getCount() > 0) {
                                vc.moveToFirst();
                                version=vc.getInt(0);
                            }
                            vc.close();
                            if (version!=DATABASE_VERSION) {
                                    Log.e(TAG,"database version mismatch");
                            }
                    }
                    c.close();


            } catch (SQLException e) {
                    Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage());
            } finally {
                    db.close();
            }
}

private void CreateDatabase(SQLiteDatabase db)
{
            try {
                    db.execSQL(DBVERSION_CREATE);
                    ContentValues args = new ContentValues();
                    args.put("version", DATABASE_VERSION);
                    db.insert(TABLE_DBVERSION, null, args);

                    db.execSQL(NOTES_CREATE);
                    // Populate with data
                    populateDataBaseFromFile();// There are probably better ways to do this.
                    setSharedPreferenceYouPopulatedDB();
            } catch (SQLException e) {
                    Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage());
            } 
}

就我个人而言,除非您确实需要,否则我不会打扰初始屏幕。

另一个想法可能是:

  1. 在db helper中编写一种确定表是否存在的方法。 如果不是,则返回false。
  2. 在启动活动中,通过调用DBHelper测试方法的请求来调用ContentProvider。
  3. 如果为false,则显示初始屏幕,然后调用Content Provider填充数据库。
  4. 如果为true,则照常进行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM