Skip to content

Instantly share code, notes, and snippets.

View cosbor11's full-sized avatar
🤓
always coding

Chris Osborn cosbor11

🤓
always coding
View GitHub Profile
@cosbor11
cosbor11 / RecipeListActivity.java
Created April 3, 2017 22:02
Persist data within an Android application
final CookBook cookBook = new CookBook();
cookBook.setTitle("Onyx's House of Yummy");
final Recipe cupOfNoodle = new Recipe();
cupOfNoodle.setContent("Cup of Noodle");
cupOfNoodle.setDetails("Open lid, add water, put powder stuff in and microwave until it boils over and makes a mess in your microwave.");
List<Recipe> recipees = new ArrayList<>();
recipees.add(cupOfNoodle);
cookBook.setRecipes(recipees);
@cosbor11
cosbor11 / RecipeListActivity.java
Created April 3, 2017 22:00
Create a persistence manager factory
PersistenceManagerFactory persistenceManagerFactory = new CacheManagerFactory();
try {
persistenceManagerFactory.initialize();
} catch (InitializationException e) {
Log.e(TAG, "Cannot initialize Persistence Manager Factory");
}
PersistenceManager persistenceManager = persistenceManagerFactory.getPersistenceManager();
@cosbor11
cosbor11 / Recipe.java
Created April 3, 2017 21:49
Recipe managed entity for Cook Book Android application
@Entity
public class Recipe extends ManagedEntity
{
@Attribute
@Identifier(generator = IdentifierGenerator.SEQUENCE)
private long recipeId;
@Attribute
private String content;
@cosbor11
cosbor11 / build.gradle
Created April 3, 2017 21:47
Add Onyx Database dependency within build.gradle
repositories {
mavenCentral()
}
...
dependencies {
compile 'com.onyxdevtools:onyx-android:1.3.0'
}
@cosbor11
cosbor11 / build.gradle
Created April 3, 2017 21:46
Android Gradle build file for configuring Android application with Onyx Database
apply plugin: 'com.android.application'
repositories {
mavenCentral()
mavenLocal()
}
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
@cosbor11
cosbor11 / Main.java
Created April 2, 2017 01:49
Remove the Query Change Listener when finished
// Remove the change listener when done
manager.removeChangeListener(hallOfFameQuarterBackQuery);
@cosbor11
cosbor11 / Main.java
Created April 2, 2017 01:47
Persist some data to see how it interacts with the Query Change Listener
// Add some data to see how the change listener reacts
Player tomBrady = new Player();
tomBrady.setFirstName("Tom");
tomBrady.setLastName("Brady");
tomBrady.setPosition("QB");
tomBrady.setHallOfFame(true);
// On Item Added should be fired
manager.saveEntity(tomBrady);
@cosbor11
cosbor11 / Main.java
Created April 2, 2017 01:47
Execute a query to register a change listener
// Execute the query to register the change listener
manager.executeQuery(hallOfFameQuarterBackQuery);
@cosbor11
cosbor11 / Main.java
Created April 2, 2017 01:46
Define a Change Listener for a Query
// Define Change listener for query
hallOfFameQuarterBackQuery.setChangeListener(new QueryListener<Player>() {
@Override
public void onItemUpdated(Player item) {
System.out.println("Player " + item.getFirstName() + " " + item.getLastName() + " has been updated!");
}
@Override
public void onItemAdded(Player item) {
System.out.println("Player " + item.getFirstName() + " " + item.getLastName() + " has been added!");
@cosbor11
cosbor11 / Main.java
Created April 2, 2017 01:44
Define a Query and Criteria for setting up a Query Change Listener
// Define critiera to match position = QB & isHallOfFame = true
QueryCriteria hallOfFameQuarterbackCriteria = new QueryCriteria("position", QueryCriteriaOperator.EQUAL, "QB")
.and(new QueryCriteria("isHallOfFame", QueryCriteriaOperator.EQUAL, true));
final Query hallOfFameQuarterBackQuery = new Query(Player.class, hallOfFameQuarterbackCriteria);