如何建立ContentProvider进行同步?

时间:2019-04-30 13:15:09

标签: android-contentprovider android-syncadapter

我要在MVP架构中设置一个Android同步适配器,并想了解如何构建它?

  1. 每个模型(建筑物,服务,用户等)是否都需要ContentProvider?

我尝试遵循开发者指南https://developer.android.com/training/sync-adapters/creating-sync-adapter。但是,我对ContentProvider不太了解!

我已经建立了数据库助手:

public class DBHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "syncAdapter.db";

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        addCarriersTable(sqLiteDatabase);
        addCompaniesTable(sqLiteDatabase);
        addUsersTable(sqLiteDatabase);
        addShiftsTable(sqLiteDatabase);
        addVehiclesTable(sqLiteDatabase);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    private void addCarriersTable(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(
                "CREATE TABLE " + CarrierEntry.TABLE_NAME + " (" +
                        CarrierEntry._ID + " INTEGER PRIMARY KEY, " +
                        CarrierEntry.COLUMN_NAME + " TEXT NOT NULL);"
        );
    }

    private void addCompaniesTable(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(
                "CREATE TABLE " + CompanyEntry.TABLE_NAME + " (" +
                        CompanyEntry._ID + " INTEGER PRIMARY KEY, " +
                        CompanyEntry.COLUMN_NAME + " TEXT NOT NULL, " +
                        CompanyEntry.COLUMN_COLOR + " TEXT NOT NULL, " +
                        CompanyEntry.COLUMN_CITY + " TEXT NOT NULL, " +
                        CompanyEntry.COLUMN_STATE + " TEXT NOT NULL);");
    }

    private void addUsersTable(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(
                "CREATE TABLE " + UserEntry.TABLE_NAME + " (" +
                        UserEntry._ID + " INTEGER PRIMARY KEY, " +
                        UserEntry.COLUMN_NAME + " TEXT NOT NULL, " +
                        UserEntry.COLUMN_ROLE + " TEXT NOT NULL, " +
                        UserEntry.COLUMN_ENROLLMENT + " TEXT NOT NULL);");
    }

    private void addShiftsTable(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(
                "CREATE TABLE " + ShiftEntry.TABLE_NAME + " (" +
                        ShiftEntry._ID + " INTEGER PRIMARY KEY, " +
                        ShiftEntry.COLUMN_NAME + " TEXT NOT NULL);");
    }

    private void addVehiclesTable(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(
                "CREATE TABLE " + VehicleEntry.TABLE_NAME + " (" +
                        VehicleEntry._ID + " INTEGER PRIMARY KEY, " +
                        VehicleEntry.COLUMN_NAME + " TEXT NOT NULL, " +
                        VehicleEntry.COLUMN_CART + " BOOLEAN NOT NULL);");
    }
}

之后,我建立了一个合同类:

public class ExampleContract {
    public static final String CONTENT_AUTHORITY = "br.com.example.syncAdapter";
    public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);

    public static final String PATH_CARRIER = "carriers";
    public static final String PATH_COMPANY = "companies";
    public static final String PATH_SHIFT = "shifts";
    public static final String PATH_USER = "users";
    public static final String PATH_VEHICLE = "vehicles";
}

但是,现在我被ContentProvider困住了。 有人可以指点我教程或给我提示吗?

0 个答案:

没有答案