當前位置: 首頁>>代碼示例>>Java>>正文


Java Room類代碼示例

本文整理匯總了Java中android.arch.persistence.room.Room的典型用法代碼示例。如果您正苦於以下問題:Java Room類的具體用法?Java Room怎麽用?Java Room使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Room類屬於android.arch.persistence.room包,在下文中一共展示了Room類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: provideWeatherDAO

import android.arch.persistence.room.Room; //導入依賴的package包/類
@Provides
@Singleton
WeatherDAO provideWeatherDAO() {
    AppDatabase database = Room.databaseBuilder(context, AppDatabase.class, "data")
            //.addMigrations(MIGRATION_1_2)
            .build();

    // If no database found on the device, a new one created via analysing Entity interfaces.
    // Migrations aren't executed even when required schema version > 1.
    // It means migrations can't be used for populating db with data.
    // Hope they change this behaviour.
    Executors.newSingleThreadExecutor().execute(() -> {
        init(database.weatherDao());
    });

    return database.weatherDao();
}
 
開發者ID:SergeyVinyar,項目名稱:AndroidNewArchitectureExample,代碼行數:18,代碼來源:AppModule.java

示例2: buildDatabase

import android.arch.persistence.room.Room; //導入依賴的package包/類
/**
 * Build the database. {@link Builder#build()} only sets up the database configuration and
 * creates a new instance of the database.
 * The SQLite database is only created when it's accessed for the first time.
 */
private static AppDatabase buildDatabase(final Context appContext,
        final AppExecutors executors) {
    return Room.databaseBuilder(appContext, AppDatabase.class, DATABASE_NAME)
            .addCallback(new Callback() {
                @Override
                public void onCreate(@NonNull SupportSQLiteDatabase db) {
                    super.onCreate(db);
                    executors.diskIO().execute(() -> {
                        // Add a delay to simulate a long-running operation
                        addDelay();
                        // Generate the data for pre-population
                        AppDatabase database = AppDatabase.getInstance(appContext, executors);
                        List<ProductEntity> products = DataGenerator.generateProducts();
                        List<CommentEntity> comments =
                                DataGenerator.generateCommentsForProducts(products);

                        insertData(database, products, comments);
                        // notify that the database was created and it's ready to be used
                        database.setDatabaseCreated();
                    });
                }
            }).build();
}
 
開發者ID:googlesamples,項目名稱:android-architecture-components,代碼行數:29,代碼來源:AppDatabase.java

示例3: onCreateView

import android.arch.persistence.room.Room; //導入依賴的package包/類
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.your_contributions, container, false);

    AppDatabase db = Room.databaseBuilder(myView.getContext(),
            AppDatabase.class,
            "restrictions").allowMainThreadQueries().build();
    RestrictionDao restrictionDao = db.getRestrictionDao();
    Log.d(TAG, restrictionDao.getAll().toString());
    List<Restriction> listOfRestrictions = restrictionDao.getAll();

    //todo: sort by latest entries first
    String allCards = "";
    for (Restriction restriction : listOfRestrictions) {
        allCards += restriction.getCard() + "\n\n";
    }

    TextView yourContributions = (TextView) myView.findViewById(R.id.yourContributionsText);
    yourContributions.setText(allCards);

    return myView;
}
 
開發者ID:curbmap,項目名稱:curbmap-android,代碼行數:24,代碼來源:YourContributionsFragment.java

示例4: getDatabase

import android.arch.persistence.room.Room; //導入依賴的package包/類
public static DataBase getDatabase(Context context, String passPhrase) {
    prefs = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
    try {
        if (null == INSTANCE) {
            if (prefs.getBoolean("first", true) && null != passPhrase) {
                createNewKeys(context, getAlias(context));
                encryptString_old(context, getAlias(context), passPhrase);
            }

            String str = decryptString_old(context, getAlias(context));
            if(null == str) return null;

            SafeHelperFactory factory=SafeHelperFactory.fromUser(Editable.Factory.getInstance().newEditable(str));
            INSTANCE = Room.databaseBuilder(context, DataBase.class, "users").openHelperFactory(factory).allowMainThreadQueries().build();
        }
    } catch (Exception e) {
        Log.e(TAG, Log.getStackTraceString(e));
    }
    return INSTANCE;
}
 
開發者ID:ceanyd,項目名稱:react-native-caller-id-android,代碼行數:21,代碼來源:DataBase.java

示例5: obtainRoomDatabase

import android.arch.persistence.room.Room; //導入依賴的package包/類
@Override
public <DB extends RoomDatabase> DB obtainRoomDatabase(Class<DB> database, String dbName) {
    if (mRoomDatabaseCache == null) {
        mRoomDatabaseCache = mCacheFactory.build(CacheType.ROOM_DATABASE_CACHE_TYPE);
    }
    Preconditions.checkNotNull(mRoomDatabaseCache, "Cannot return null from a Cache.Factory#build(int) method");
    DB roomDatabase;
    synchronized (mRoomDatabaseCache) {
        roomDatabase = (DB) mRoomDatabaseCache.get(database.getName());
        if (roomDatabase == null) {
            RoomDatabase.Builder builder = Room.databaseBuilder(mApplication, database, dbName);
            //Customize the room configuration
            if (mRoomConfiguration != null) {
                mRoomConfiguration.configRoom(mApplication, builder);
            }
            roomDatabase = (DB) builder.build();
            mRoomDatabaseCache.put(database.getName(), roomDatabase);
        }
    }
    return roomDatabase;
}
 
開發者ID:goutham106,項目名稱:GmArchMvvm,代碼行數:22,代碼來源:RepositoryManager.java

示例6: getDatabase

import android.arch.persistence.room.Room; //導入依賴的package包/類
static WordRoomDatabase getDatabase(final Context context) {
    if (INSTANCE == null) {
        synchronized (WordRoomDatabase.class) {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                        WordRoomDatabase.class, "word_database")
                        // Wipes and rebuilds instead of migrating if no Migration object.
                        // Migration is not part of this codelab.
                        .fallbackToDestructiveMigration()
                        .addCallback(sRoomDatabaseCallback)
                        .build();
            }
        }
    }
    return INSTANCE;
}
 
開發者ID:googlecodelabs,項目名稱:android-room-with-a-view,代碼行數:17,代碼來源:WordRoomDatabase.java

示例7: obtainRoomDatabase

import android.arch.persistence.room.Room; //導入依賴的package包/類
@Override
public <DB extends RoomDatabase> DB obtainRoomDatabase(Class<DB> database, String dbName) {
    if (mRoomDatabaseCache == null) {
        mRoomDatabaseCache = mCacheFactory.build(CacheType.ROOM_DATABASE_CACHE_TYPE);
    }
    Preconditions.checkNotNull(mRoomDatabaseCache, "Cannot return null from a Cache.Factory#build(int) method");
    DB roomDatabase;
    synchronized (mRoomDatabaseCache) {
        roomDatabase = (DB) mRoomDatabaseCache.get(database.getName());
        if (roomDatabase == null) {
            RoomDatabase.Builder builder = Room.databaseBuilder(mApplication, database, dbName);
            //自定義 Room 配置
            if (mRoomConfiguration != null) {
                mRoomConfiguration.configRoom(mApplication, builder);
            }
            roomDatabase = (DB) builder.build();
            mRoomDatabaseCache.put(database.getName(), roomDatabase);
        }
    }
    return roomDatabase;
}
 
開發者ID:xiaobailong24,項目名稱:MVVMArms,代碼行數:22,代碼來源:RepositoryManager.java

示例8: getAppDatabase

import android.arch.persistence.room.Room; //導入依賴的package包/類
public static AppDatabase getAppDatabase(Context context, boolean force) {
        if (INSTANCE == null || force) {

            INSTANCE =
                    Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "justintrain.db")
                            // allow queries on the main thread.
                            // Don't do\ this on a real app! See PersistenceBasicSample for an example.
                            .openHelperFactory(new AssetSQLiteOpenHelperFactory())
//                            .addMigrations(MIGRATION_1_2)
                            .allowMainThreadQueries()
                            .build();
        }
        return INSTANCE;
    }
 
開發者ID:albertogiunta,項目名稱:justintrain-client-android,代碼行數:15,代碼來源:AppDatabase.java

示例9: initDatabase

import android.arch.persistence.room.Room; //導入依賴的package包/類
private void initDatabase() {
    db = Room.databaseBuilder(getApplicationContext(),
            Database.class, "database-name-pow")
            .fallbackToDestructiveMigration()
            .build();
    dbExecutor = Executors.newSingleThreadExecutor();
}
 
開發者ID:ponewheel,項目名稱:android-ponewheel,代碼行數:8,代碼來源:App.java

示例10: getLocationByUser

import android.arch.persistence.room.Room; //導入依賴的package包/類
public LiveData<List<LocationInfo>> getLocationByUser(Context context, String user) {
    if (locationInfoList == null) {
        LocationInfoDatabase db = Room.databaseBuilder(context,
                LocationInfoDatabase.class, LocationDbKey.DATABASE_NAME).build();
        locationInfoList = db.locationInfoDao().getLocationByUser(user);
    }
    return locationInfoList;
}
 
開發者ID:akexorcist,項目名稱:Android-ArchComponents,代碼行數:9,代碼來源:MainActivityViewModel.java

示例11: getDatabase

import android.arch.persistence.room.Room; //導入依賴的package包/類
@MainThread
public AppDatabase getDatabase() {
    if (database == null) {
        database = Room.databaseBuilder(this, AppDatabase.class, "FitnessHabits-database").fallbackToDestructiveMigration().build(); //TODO: remove fallback destroy
    }
    return database;
}
 
開發者ID:strudelauxpommes,項目名稱:FitnessHabits,代碼行數:8,代碼來源:FitnessHabitsApplication.java

示例12: getInstance

import android.arch.persistence.room.Room; //導入依賴的package包/類
public static UploadHistoryRoomDatabase getInstance(Context context) {
    if (INSTANCE == null) {
        synchronized (UploadHistoryRoomDatabase.class) {
            if (INSTANCE == null) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                        UploadHistoryRoomDatabase.class,
                        "upload_history")
                        .build();
            }
        }
    }

    return INSTANCE;
}
 
開發者ID:rumaan,項目名稱:file.io-app,代碼行數:15,代碼來源:UploadHistoryRoomDatabase.java

示例13: initializeDb

import android.arch.persistence.room.Room; //導入依賴的package包/類
@Before
public void initializeDb() {
    database = Room.inMemoryDatabaseBuilder(
            InstrumentationRegistry.getContext(),
            UploadHistoryRoomDatabase.class)
            .build();
}
 
開發者ID:rumaan,項目名稱:file.io-app,代碼行數:8,代碼來源:UploadItemDaoTest.java

示例14: init

import android.arch.persistence.room.Room; //導入依賴的package包/類
@Before
public void init() {
    database = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(), UploadHistoryRoomDatabase.class).build();
    for (int i = 0; i < 5; i++) {
        UploadItem uploadItem = new UploadItem("test file name " + i, "test URL " + i, Consts.DEFAULT_EXPIRE_WEEKS);
        database.uploadItemDao().insert(uploadItem);
    }

}
 
開發者ID:rumaan,項目名稱:file.io-app,代碼行數:10,代碼來源:UploadHistoryInstrumentedTest.java

示例15: getDatabase

import android.arch.persistence.room.Room; //導入依賴的package包/類
public static AppDatabase getDatabase(Context context)
{
    if(INSTANCE == null)
    {
        INSTANCE = Room.databaseBuilder(context, AppDatabase.class, "LabTabletDB").build();
    }
    return INSTANCE;
}
 
開發者ID:feup-infolab,項目名稱:labtablet,代碼行數:9,代碼來源:AppDatabase.java


注:本文中的android.arch.persistence.room.Room類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。