Android DB and Async Task






Ivan Kocijan - ikocijan0@gmail.com

Milan Jovanović -milan.jovanovic24@gmail.com

Content


  • Intent extras
  • Async task
  • SQLite database
  • Active Android

Intent extra


  • A Bundle of  any additional information
  • Used to provide extended information

Intent intent = new Intent(MainActivity.this, ReadFromDbActivity.class);
intent.putExtra("key", "value");
startActivity(intent);
getIntent().getExtras().get("key");

Async task

Executing Async task






Basics

  • Android uses SQLite database
  • Android stores your database in private disk space that's associated with you application
  • Define Schema and Contract
  • Add
  • Read
  • Delete
  • Update

Add data to DB

DbHelper mDbHelper = new DbHelper(Context);SQLiteDatabase db = mDbHelper.getWritableDatabase();

ContentValues values = new ContentValues(); values.put(DBExampleContract.DBEntry.COLUMN_USER_NAME, name.getText().toString()); values.put(DBExampleContract.DBEntry.COLUMN_USER_SURNAME, surname.getText().toString()); values.put(DBExampleContract.DBEntry.USER_DESCRIPTION, description.getText().toString());
db.insert(DBExampleContract.DBEntry.TABLE_NAME, null,values);


Read from database

 SQLiteDatabase db = mDbHelper.getReadableDatabase();        //Which columns from the database will be used
        String[] projection = {
               DBExampleContract.DBEntry.COLUMN_USER_NAME,
               DBExampleContract.DBEntry.COLUMN_USER_SURNAME,
               DBExampleContract.DBEntry.USER_DESCRIPTION
        };
 Cursor cursor = db.query(
 DBExampleContract.DBEntry.TABLE_NAME, // The table to query
         projection, // The columns to return
         null, // The columns for the WHERE clause
         null, // The values for the WHERE clause         null, // don't group the rows
         null, // don't filter by row groups
         null // The sort order
        );
cursor.moveToFirst();
while (cursor.moveToNext()) { cursor.getString(cursor.getColumnIndex (DBExampleContract.DBEntry.COLUMN_USER_NAME));}

Delete data from db

 // Where part of query
String selection = DBExampleContract.DBEntry.COLUMN_USER_NAME                   + " LIKE ?";

String[] selectionArgs = { String.valueOf(userName.getText().toString())};db.delete(DBExampleContract.DBEntry.TABLE_NAME, selection, selectionArgs);

But this is complicated...

  • We are lazy and we do not want to write so much code
  • This is hard to read
  • Adds a lot of boilerplate code 
  • There is a solution for this and it's called Object-relational mapping (ORM) 

ORM for Android

Adding Active Android


  • Add jar file as a dependency in build gradle 


dependencies { 
     compile files('libs/activeandroid.jar')}
  • Add android name and version to AndroidManifest

<manifest ...>
    <application     android:name="ninja.ofca.dolly" ...>
<meta-data android:name="AA_DB_NAME"           android:value="Dolly.db" /><meta-data android:name="AA_DB_VERSION"           android:value="5" />

    </application>
</manifest>
  • Initialize ActiveAndroid in Application class

public class OfcaApplication extends Applicaton {
           @Override
    public void onCreate() {
        super.onCreate();
       
        ActiveAndroid.initialize(this);
    }
}

How to use Active android

  • Create your model, extend Model class and add anotations
  • Your model can have multiple constructors but you will have to declare empty constructor in that case

Add data to DB with Active Android





Read data from DB with Active Android





Delete data from DB with Active Android




Homework

  • Save data downloaded from weather service to database

Materials from this workshop


Good read

Android DB and Async Task - Workshop 4

By Ivan Kocijan Koc

Android DB and Async Task - Workshop 4

Intent extra, Async task, SQLite database, Active Android library

  • 2,266