Skip to content

Instantly share code, notes, and snippets.

Avatar
🤓
always coding

Chris Osborn cosbor11

🤓
always coding
View GitHub Profile
@cosbor11
cosbor11 / RecipeListActivity.java
Created Apr 3, 2017
Persist data within an Android application
View RecipeListActivity.java
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 Apr 3, 2017
Create a persistence manager factory
View RecipeListActivity.java
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 Apr 3, 2017
Recipe managed entity for Cook Book Android application
View Recipe.java
@Entity
public class Recipe extends ManagedEntity
{
@Attribute
@Identifier(generator = IdentifierGenerator.SEQUENCE)
private long recipeId;
@Attribute
private String content;
@cosbor11
cosbor11 / build.gradle
Created Apr 3, 2017
Add Onyx Database dependency within build.gradle
View build.gradle
repositories {
mavenCentral()
}
...
dependencies {
compile 'com.onyxdevtools:onyx-android:1.3.0'
}
@cosbor11
cosbor11 / build.gradle
Created Apr 3, 2017
Android Gradle build file for configuring Android application with Onyx Database
View build.gradle
apply plugin: 'com.android.application'
repositories {
mavenCentral()
mavenLocal()
}
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
@cosbor11
cosbor11 / Main.java
Created Apr 2, 2017
Remove the Query Change Listener when finished
View Main.java
// Remove the change listener when done
manager.removeChangeListener(hallOfFameQuarterBackQuery);
@cosbor11
cosbor11 / Main.java
Created Apr 2, 2017
Persist some data to see how it interacts with the Query Change Listener
View Main.java
// 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 Apr 2, 2017
Execute a query to register a change listener
View Main.java
// Execute the query to register the change listener
manager.executeQuery(hallOfFameQuarterBackQuery);
@cosbor11
cosbor11 / Main.java
Created Apr 2, 2017
Define a Change Listener for a Query
View Main.java
// 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 Apr 2, 2017
Define a Query and Criteria for setting up a Query Change Listener
View Main.java
// 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);
You can’t perform that action at this time.