Membuat Aplikasi CRUD SQLite dengan Content Provider dan Circular Reveal Animation di Android Part2

di part pertama kita sudah melakukan persiapan untuk project ini, selanjutnya kita akan membuat class DBHelper di java



pertama buat dulu package data di java kemudian buat class EmployeeDbHelper dan masukan kode di bawah:
package com.giviews.employee.data;

import
android.content.Context;
import
android.database.DatabaseErrorHandler;
import
android.database.sqlite.SQLiteDatabase;
import
android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by asus on 22/10/2017.
 */

public final class EmployeeDbHelper extends SQLiteOpenHelper{

   
public static final String LOG_TAG = EmployeeDbHelper.class.getSimpleName();

    public  static final
String DATABASE_NAME = "employee.db";

    private static final int
DATABASE_VERSION = 1;

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

   
@Override
   
public void onCreate(SQLiteDatabase db) {
       
//Create String to contains the SQL table
       
String SQL_CREATE_EMPLOYEES_TABLE = "CREATE TABLE " + EmployeeContract.EmployeeEntry.TABLE_NAME + "("
               
+ EmployeeContract.EmployeeEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
               
+ EmployeeContract.EmployeeEntry.COLUMN_FIRSTNAME + " TEXT NOT NULL, "
               
+ EmployeeContract.EmployeeEntry.COLUMN_LASTNAME + " TEXT NOT NULL, "
               
+ EmployeeContract.EmployeeEntry.COLUMN_TITLE + " TEXT NOT NULL, "
               
+ EmployeeContract.EmployeeEntry.COLUMN_DEPARTMENT + " TEXT NOT NULL, "
               
+ EmployeeContract.EmployeeEntry.COLUMN_CITY + " TEXT NOT NULL, "
               
+ EmployeeContract.EmployeeEntry.COLUMN_PHONE + " TEXT NOT NULL, "
               
+ EmployeeContract.EmployeeEntry.COLUMN_IMAGE + " BLOB NOT NULL, "
               
+ EmployeeContract.EmployeeEntry.COLUMN_EMAIL + " TEXT NOT NULL, "
               
+ EmployeeContract.EmployeeEntry.COLUMN_EMPLOYEE_GENDER + " INTEGER NOT NULL" + " );";

       
//EXECUTE THE SQL STATEMENT
       
db.execSQL(SQL_CREATE_EMPLOYEES_TABLE);
   
}

   
@Override
   
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
       
//on upgrade
   
}
}

pada kelas DbHelper di atas kita sudah membuat database dan tablenya.
Sekarang kita buat class baru dengan nama EmployeeContract dan masukan kode berikut:
package com.giviews.employee.data;



import android.content.Context;

import android.database.DatabaseErrorHandler;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;



/**

 * Created by asus on 22/10/2017.

 */



public final class EmployeeDbHelper extends SQLiteOpenHelper{



    public static final String LOG_TAG = EmployeeDbHelper.class.getSimpleName();



    public  static final String DATABASE_NAME = "employee.db";



    private static final int DATABASE_VERSION = 1;



    public EmployeeDbHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }



    @Override

    public void onCreate(SQLiteDatabase db) {

        //Create String to contains the SQL table

        String SQL_CREATE_EMPLOYEES_TABLE = "CREATE TABLE " + EmployeeContract.EmployeeEntry.TABLE_NAME + "("

                + EmployeeContract.EmployeeEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "

                + EmployeeContract.EmployeeEntry.COLUMN_FIRSTNAME + " TEXT NOT NULL, "

                + EmployeeContract.EmployeeEntry.COLUMN_LASTNAME + " TEXT NOT NULL, "

                + EmployeeContract.EmployeeEntry.COLUMN_TITLE + " TEXT NOT NULL, "

                + EmployeeContract.EmployeeEntry.COLUMN_DEPARTMENT + " TEXT NOT NULL, "

                + EmployeeContract.EmployeeEntry.COLUMN_CITY + " TEXT NOT NULL, "

                + EmployeeContract.EmployeeEntry.COLUMN_PHONE + " TEXT NOT NULL, "

                + EmployeeContract.EmployeeEntry.COLUMN_IMAGE + " BLOB NOT NULL, "

                + EmployeeContract.EmployeeEntry.COLUMN_EMAIL + " TEXT NOT NULL, "

                + EmployeeContract.EmployeeEntry.COLUMN_EMPLOYEE_GENDER + " INTEGER NOT NULL" + " );";



        //EXECUTE THE SQL STATEMENT

        db.execSQL(SQL_CREATE_EMPLOYEES_TABLE);

    }



    @Override

    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {

        //on upgrade

    }

}

kemudian buat lagi class baru dengan nama EmployeeProvider dan masukan kode berikut:
package com.giviews.employee.data;



import android.app.SearchManager;

import android.content.ContentProvider;

import android.content.ContentUris;

import android.content.ContentValues;

import android.content.UriMatcher;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteQueryBuilder;

import android.net.Uri;

import android.util.Log;



import java.util.HashMap;



/**

 * Created by asus on 24/10/2017.

 */



public class EmployeeProvider extends ContentProvider {



    public static final String LOG_TAG = EmployeeProvider.class.getSimpleName();



    private static final int EMPLOYEES = 100;



    private static final int EMPLOYEE_ID = 101;



    private static final int SEARCH_SUGGEST = 102;



    private static final HashMap<String, String> SEARCH_SUGGEST_PROJECTION_MAP;

    static {

        SEARCH_SUGGEST_PROJECTION_MAP = new HashMap<String, String>();

        SEARCH_SUGGEST_PROJECTION_MAP.put(EmployeeContract.EmployeeEntry._ID, EmployeeContract.EmployeeEntry._ID);

        SEARCH_SUGGEST_PROJECTION_MAP.put(SearchManager.SUGGEST_COLUMN_TEXT_1, EmployeeContract.EmployeeEntry.COLUMN_FIRSTNAME + " AS " + SearchManager.SUGGEST_COLUMN_TEXT_1);

        SEARCH_SUGGEST_PROJECTION_MAP.put(SearchManager.SUGGEST_COLUMN_TEXT_2, EmployeeContract.EmployeeEntry.COLUMN_LASTNAME + " AS " + SearchManager.SUGGEST_COLUMN_TEXT_2);

        SEARCH_SUGGEST_PROJECTION_MAP.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, EmployeeContract.EmployeeEntry._ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID);

    }



    private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);



    public static final Uri SEARCH_SUGGEST_URI = Uri.parse("content://" + EmployeeContract.CONTENT_ATHORITY + "/" + EmployeeContract.PATH_EMPLOYEES + "/" + SearchManager.SUGGEST_URI_PATH_QUERY);



    static {

        sUriMatcher.addURI(EmployeeContract.CONTENT_ATHORITY, EmployeeContract.PATH_EMPLOYEES, EMPLOYEES);



        sUriMatcher.addURI(EmployeeContract.CONTENT_ATHORITY, EmployeeContract.PATH_EMPLOYEES + "/#", EMPLOYEE_ID);

    }



    public EmployeeProvider(){

        sUriMatcher.addURI(EmployeeContract.CONTENT_ATHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);

        sUriMatcher.addURI(EmployeeContract.CONTENT_ATHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGEST);

    }



    private EmployeeDbHelper mDbHelper;



    @Override

    public boolean onCreate() {

        mDbHelper = new EmployeeDbHelper(getContext());

        return true;

    }



    @Override

    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,

                        String sortOrder) {



        SQLiteDatabase database = mDbHelper.getReadableDatabase();



        Cursor cursor;



        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

        queryBuilder.setTables(EmployeeContract.EmployeeEntry.TABLE_NAME);



        int match = sUriMatcher.match(uri);

        switch (match) {

            case SEARCH_SUGGEST:

                selectionArgs = new String[] { "%" + selectionArgs[0] + "%", "%" + selectionArgs[0] + "%"};

                queryBuilder.setProjectionMap(SEARCH_SUGGEST_PROJECTION_MAP);



                cursor = queryBuilder.query(database, projection, selection, selectionArgs, null, null, sortOrder);

                break;

            case EMPLOYEES:

                cursor = database.query(EmployeeContract.EmployeeEntry.TABLE_NAME, projection, selection, selectionArgs,

                        null, null, sortOrder);

                break;

            case EMPLOYEE_ID:

                selection = EmployeeContract.EmployeeEntry._ID + "=?";

                selectionArgs = new String[] {String.valueOf(ContentUris.parseId(uri))};



                cursor = database.query(EmployeeContract.EmployeeEntry.TABLE_NAME, projection, selection, selectionArgs,

                        null, null, sortOrder);

                break;

            default:

                throw new IllegalArgumentException("Cannot query unknown uri" + uri);

        }

        cursor.setNotificationUri(getContext().getContentResolver(), uri);

        return cursor;

    }



    @Override

    public Uri insert(Uri uri, ContentValues contentValues) {

        final int match = sUriMatcher.match(uri);

        switch (match) {

            case EMPLOYEES:

                return insertEmployee(uri, contentValues);

            default:

                throw new IllegalArgumentException("Inserted is not supported for " + uri);

        }

    }



    private Uri insertEmployee(Uri uri, ContentValues values) {

        String firstname = values.getAsString(EmployeeContract.EmployeeEntry.COLUMN_FIRSTNAME);

        if (firstname == null) {

            throw new IllegalArgumentException("Employee require a firstname");

        }



        String lastname = values.getAsString(EmployeeContract.EmployeeEntry.COLUMN_LASTNAME);

        if (lastname == null) {

            throw new IllegalArgumentException("Employee requires a lastname");

        }



        String department = values.getAsString(EmployeeContract.EmployeeEntry.COLUMN_DEPARTMENT);

        if (department == null) {

            throw new IllegalArgumentException("Employee requires a lastname");

        }



        String title = values.getAsString(EmployeeContract.EmployeeEntry.COLUMN_TITLE);

        if (title == null) {

            throw new IllegalArgumentException("Employee requires a title");

        }



        String city = values.getAsString(EmployeeContract.EmployeeEntry.COLUMN_CITY);

        if (city == null) {

            throw  new IllegalArgumentException("Employee requires a city");

        }



        String phone = values.getAsString(EmployeeContract.EmployeeEntry.COLUMN_PHONE);

        if (phone == null) {

            throw new IllegalArgumentException("Employee requires a phone number");

        }



        String email = values.getAsString(EmployeeContract.EmployeeEntry.COLUMN_EMAIL);

        if (email == null) {

            throw new IllegalArgumentException("Employee requires an email");

        }



        //check gender is valid

        Integer gender = values.getAsInteger(EmployeeContract.EmployeeEntry.COLUMN_EMPLOYEE_GENDER);

        if (gender == null || !EmployeeContract.EmployeeEntry.isValidGender(gender)) {

            throw new IllegalArgumentException("Employee requires valid gender");

        }



        //get writable database

        SQLiteDatabase database = mDbHelper.getWritableDatabase();



        //Insert new Employee with given values

        Long id = database.insert(EmployeeContract.EmployeeEntry.TABLE_NAME, null, values);

        if (id == -1) {

            Log.e(LOG_TAG, "Failed to insert row for" + uri);

            return null;

        }



        getContext().getContentResolver().notifyChange(uri, null);



        return ContentUris.withAppendedId(uri, id);

    }



    @Override

    public int update(Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) {

        final int match = sUriMatcher.match(uri);

        switch (match) {

            case EMPLOYEES:

                return updateEmployee(uri, contentValues, selection, selectionArgs);

            case EMPLOYEE_ID:

                selection = EmployeeContract.EmployeeEntry._ID + "=?";

                selectionArgs = new String[] {String.valueOf(ContentUris.parseId(uri))};

                return updateEmployee(uri, contentValues, selection, selectionArgs);

            default:

                throw new IllegalArgumentException("Update is not supported for " + uri);

        }

    }



    private int updateEmployee(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

        if (values.containsKey(EmployeeContract.EmployeeEntry.COLUMN_FIRSTNAME)){

            String firstname = values.getAsString(EmployeeContract.EmployeeEntry.COLUMN_FIRSTNAME);

            if (firstname == null) {

                throw new IllegalArgumentException("Employee requires a firstname");

            }

        }



        if (values.containsKey(EmployeeContract.EmployeeEntry.COLUMN_LASTNAME)){

            String lastname = values.getAsString(EmployeeContract.EmployeeEntry.COLUMN_LASTNAME);

            if (lastname == null) {

                throw new IllegalArgumentException("Employess requires a lastname");

            }

        }



        if (values.containsKey(EmployeeContract.EmployeeEntry.COLUMN_TITLE)){

            String title = values.getAsString(EmployeeContract.EmployeeEntry.COLUMN_TITLE);

            if (title == null) {

                throw new IllegalArgumentException("Employee requires a title");

            }

        }



        if (values.containsKey(EmployeeContract.EmployeeEntry.COLUMN_DEPARTMENT)) {

            String department = values.getAsString(EmployeeContract.EmployeeEntry.COLUMN_DEPARTMENT);

            if (department == null) {

                throw new IllegalArgumentException("Employee requires a department");

            }

        }



        if (values.containsKey(EmployeeContract.EmployeeEntry.COLUMN_CITY)) {

            String city = values.getAsString(EmployeeContract.EmployeeEntry.COLUMN_CITY);

            if (city == null) {

                throw new IllegalArgumentException("Employee requires a city");

            }

        }



        if (values.containsKey(EmployeeContract.EmployeeEntry.COLUMN_PHONE)) {

            String phone = values.getAsString(EmployeeContract.EmployeeEntry.COLUMN_PHONE);

            if (phone == null) {

                throw new IllegalArgumentException("Employee requires a phone number");

            }

        }



        if (values.containsKey(EmployeeContract.EmployeeEntry.COLUMN_EMAIL)) {

            String email = values.getAsString(EmployeeContract.EmployeeEntry.COLUMN_EMAIL);

            if (email == null) {

                throw new IllegalArgumentException("Employee require an email");

            }

        }



        if (values.containsKey(EmployeeContract.EmployeeEntry.COLUMN_EMPLOYEE_GENDER)) {

            Integer gender = values.getAsInteger(EmployeeContract.EmployeeEntry.COLUMN_EMPLOYEE_GENDER);

            if (gender == null || !EmployeeContract.EmployeeEntry.isValidGender(gender)) {

                throw new IllegalArgumentException("Employee require a valid gender");

            }

        }



        if (values.size() == 0) {

            return 0;

        }



        SQLiteDatabase database = mDbHelper.getWritableDatabase();



        int rowsUpdated = database.update(EmployeeContract.EmployeeEntry.TABLE_NAME, values, selection, selectionArgs);



        if (rowsUpdated != 0) {

            getContext().getContentResolver().notifyChange(uri, null);

        }



        return rowsUpdated;

    }



    @Override

    public int delete(Uri uri, String selection, String[] selectionArgs) {

        SQLiteDatabase database = mDbHelper.getWritableDatabase();



        int rowsDeleted;



        final int match = sUriMatcher.match(uri);

        switch (match) {

            case EMPLOYEES:

                //delete all rows

                rowsDeleted = database.delete(EmployeeContract.EmployeeEntry.TABLE_NAME, selection, selectionArgs);

                break;

            case EMPLOYEE_ID:

                selection = EmployeeContract.EmployeeEntry._ID + "=?";

                selectionArgs = new String[] {String.valueOf(ContentUris.parseId(uri))};

                rowsDeleted = database.delete(EmployeeContract.EmployeeEntry.TABLE_NAME, selection, selectionArgs);

                break;

            default:

                throw new IllegalArgumentException("Deletion is not support for " + uri);

        }



        if (rowsDeleted != 0) {

            getContext().getContentResolver().notifyChange(uri, null);

        }



        return rowsDeleted;

    }



    @Override

    public String getType(Uri uri) {

        final  int match = sUriMatcher.match(uri);

        switch (match) {

            case EMPLOYEES:

                return EmployeeContract.EmployeeEntry.CONTENT_LIST_TYPE;

            case EMPLOYEE_ID:

                return EmployeeContract.EmployeeEntry.CONTENT_ITEM_TYPE;

            case SEARCH_SUGGEST:

                return null;

            default:

                throw new IllegalArgumentException("Unknown URI " + uri + " with match " + match);

        }

    }

}

pada class EmployeeProvider diatas kita sudah membuat fungsi untuk CRUD nya yaitu fungsi untuk menambahkan data, merubah data, memparsing data dan menghapus data.

setelah kita membuat class-class diatas selanjutnya kita akan membuat desain antarmuka untuk aplikasi kita

pertama kita buat layout activity_employee seperti berikut:
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.giviews.employee.EmployeeActivity">



    <android.support.v7.widget.Toolbar

        android:id="@+id/toolbar_setting"

        android:layout_width="match_parent"

        android:layout_height="?attr/actionBarSize"

        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

        android:theme="@style/AppTheme.AppBarOverlay" />



    <ListView

        android:id="@+id/list"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true"

        android:layout_below="@+id/toolbar_setting">



    </ListView>



    <!--Empty List-->

    <RelativeLayout

        android:id="@+id/empty_view"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true">

        <ImageView

            android:id="@+id/img"

            android:layout_width="100dp"

            android:layout_height="100dp"

            android:src="@drawable/ic_notifications"

            android:layout_centerInParent="true"/>

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Belum Ada Data"

            android:gravity="center"

            android:layout_below="@+id/img" />



    </RelativeLayout>



    <android.support.design.widget.FloatingActionButton

        android:id="@+id/fab"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_margin="16dp"

        android:layout_alignParentBottom="true"

        android:layout_alignParentRight="true"

        android:src="@drawable/ic_person_add"/>

</RelativeLayout>

selanjutnya buat layout activity_editor seperti berikut:
<?xml version="1.0" encoding="utf-8"?>

<com.hendraanggrian.widget.RevealFrameLayout

    android:id="@+id/revealLayout"

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.giviews.employee.EmployeeEditor">



    <android.support.v7.widget.Toolbar

        android:id="@+id/toolbar_setting"

        android:layout_width="match_parent"

        android:layout_height="?attr/actionBarSize"

        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

        android:theme="@style/AppTheme.AppBarOverlay" />



    <android.support.design.widget.CoordinatorLayout

        android:layout_marginTop="56dp"

        android:id="@+id/root_layout"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:background="@color/colorAccent">



        <ScrollView

            android:layout_width="match_parent"

            android:layout_height="match_parent">



            <LinearLayout

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:padding="20dp"

                android:orientation="vertical">

                

                <ImageView

                    android:id="@+id/profileImageView"

                    android:layout_width="200dp"

                    android:layout_height="200dp"

                    android:layout_gravity="center"

                    android:background="@color/colorAccent"

                    android:scaleType="centerCrop"

                    android:src="@drawable/ic_account_circle_black"/>



                <Button

                    android:layout_margin="10dp"

                    android:layout_gravity="center"

                    android:id="@+id/pickImage"

                    android:layout_width="90dp"

                    android:layout_height="wrap_content"

                    android:text="Select Image" />



                <EditText

                    android:id="@+id/firstName"

                    android:layout_width="match_parent"

                    android:layout_height="wrap_content"

                    android:hint="Fist Name"/>



                <EditText

                    android:id="@+id/lastName"

                    android:layout_width="match_parent"

                    android:layout_height="wrap_content"

                    android:hint="Last Name"/>



                <EditText

                    android:id="@+id/title"

                    android:layout_width="match_parent"

                    android:layout_height="wrap_content"

                    android:hint="Title"/>



                <EditText

                    android:id="@+id/department"

                    android:layout_width="match_parent"

                    android:layout_height="wrap_content"

                    android:hint="Department"/>



                <EditText

                    android:id="@+id/city"

                    android:layout_width="match_parent"

                    android:layout_height="wrap_content"

                    android:hint="City"/>



                <EditText

                    android:id="@+id/phone"

                    android:layout_width="match_parent"

                    android:layout_height="wrap_content"

                    android:inputType="number"

                    android:hint="Phone Number"/>



                <EditText

                    android:id="@+id/email"

                    android:inputType="textEmailAddress"

                    android:layout_width="match_parent"

                    android:layout_height="wrap_content"

                    android:hint="Email"/>



                <Spinner

                    android:id="@+id/spinner_gender"

                    android:layout_width="wrap_content"

                    android:layout_height="32dp"

                    android:paddingRight="16dp"

                    android:spinnerMode="dropdown"/>

            </LinearLayout>

        </ScrollView>

    </android.support.design.widget.CoordinatorLayout>



</com.hendraanggrian.widget.RevealFrameLayout>

selanjutnya buat layout list_item untuk menampilkan data listviewnya:
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="16dp">



    <TextView

        android:id="@+id/firstname"

        android:textStyle="bold"

        android:textSize="17dp"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />



    <TextView

        android:id="@+id/lastname"

        android:textStyle="bold"

        android:layout_marginLeft="5dp"

        android:textSize="17dp"

        android:layout_toEndOf="@+id/firstname"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_toRightOf="@+id/firstname" />



    <TextView

        android:id="@+id/titleview"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/lastname"/>







</RelativeLayout>


sampai disini dulu part ke2 nya akan kita lanjutkan di part ke 3 memfungsikan layout yang sudah kita buat

Membuat Aplikasi CRUD SQLite dengan Content Provider dan Circular Reveal Animation di Android Part1



Sebelumnya kita sudah membuat Aplikasi CRUD dengan SQLite sederhana, kali ini kita akan membuat CRUD dengan Content Provider dan akan kita tambahkan beberapa fitur seperti circular reveal animation material color picker, butterknife, kotlin untuk menjalankan library circular revealnya. jika anda masih menggunakan android studio 2.3 ke bawah anda harus menambahkan dulu plugin kotlinnya. dan akan ditambahkan juga foto, akan kita bahas bagaimana cara memasukan foto ke database SQLite.

demo Aplikasi CRUD SQLite di Android denganContent Provider dan Circular Reveal Animation

Langkah pertama Persiapan

1.buatlah projek baru kemudian tambahkan beberapa library berikut:
compile 'com.android.support:design:26.1.0'
compile 'com.hendraanggrian:reveallayout:0.5.3'
compile 'com.afollestad.material-dialogs:core:0.9.4.5'
compile 'com.android.support:support-v4:26.1.0'
compile 'com.turki-alkhateeb:materialcolorpicker:1.0.7'
compile 'com.jakewharton:butterknife:8.7.0'

compile 'com.hendraanggrian:bundler:0.5.0'

compile 'com.android.support.constraint:constraint-layout:1.0.2'
kapt 'com.jakewharton:butterknife-compiler:8.7.0'

kapt 'com.hendraanggrian:bundler-compiler:0.5.0'

2. di arrays.xml tambahkan kode berikut, array ini natinya akan digunakan untuk mengisi nama gender dan pilihan untuk material dialog
<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string-array name="array_gender_options">

        <item>@string/gender_unknown</item>

        <item>@string/gender_male</item>

        <item>@string/gender_female</item>

    </string-array>



    <string-array name="upload_images">

        <item>Pick from gallery</item>

        <item>Click from Camera</item>

        <item>Remove Image</item>

    </string-array>

    

    <string-array name="itemIds">

        <item>gallery_image</item>

        <item>camera_image</item>

        <item>remove_image</item>

    </string-array>

</resources>

3. tambahkan beberapa warna di file colors.xml seperti berikut, warna-warna ini natinya akan digunakan untuk warna thema nya, themanya nanti akan bisa diganti-ganti:
<?xml version="1.0" encoding="utf-8"?>

<resources>

    <color name="colorPrimary">#3F51B5</color>

    <color name="colorPrimaryDark">#303F9F</color>

    <color name="colorAccent">#FF4081</color>

    <color name="colorShop">#9a1e1b</color>

    <color name="colorShop2">#071f53</color>

    <color name="colorNavText">#ffffff</color>

    <color name="colorSettings">#40000000</color>

    <color name="colorClick">#abcdef</color>

</resources>

4. tambahkan beberapa item di file string.xml
<resources>

    <string name="app_name">Employee</string>

    <string name="gender_male">Male</string>

    <string name="gender_female">Female</string>

    <string name="gender_unknown">Unknown</string>

</resources>

5. tambahkan beberapa thema di style.xml
<resources>



    <!-- Base application theme. -->

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/colorPrimary</item>

        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

        <item name="colorAccent">@color/colorAccent</item>

    </style>



    <style name="AppTheme_red" parent="Theme.AppCompat.Light.NoActionBar">

        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/red_colorPrimary</item>

        <item name="colorPrimaryDark">@color/red_colorPrimaryDark</item>

        <item name="colorAccent">@color/red_colorAccent</item>

    </style>



    <style name="AppTheme_pink" parent="Theme.AppCompat.Light.NoActionBar">

        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/pink_colorPrimary</item>

        <item name="colorPrimaryDark">@color/pink_colorPrimaryDark</item>

        <item name="colorAccent">@color/pink_colorAccent</item>

    </style>



    <style name="AppTheme_darpink" parent="Theme.AppCompat.Light.NoActionBar">

        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/darkpink_colorPrimary</item>

        <item name="colorPrimaryDark">@color/darkpink_colorPrimary</item>

        <item name="colorAccent">@color/darkpink_colorAccent</item>

    </style>



    <style name="AppTheme_violet" parent="Theme.AppCompat.Light.NoActionBar">

        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/violet_colorPrimary</item>

        <item name="colorPrimaryDark">@color/violet_colorPrimaryDark</item>

        <item name="colorAccent">@color/violet_colorAccent</item>

    </style>



    <style name="AppTheme_blue" parent="Theme.AppCompat.Light.NoActionBar">

        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/blue_colorPrimary</item>

        <item name="colorPrimaryDark">@color/blue_colorPrimaryDark</item>

        <item name="colorAccent">@color/blue_colorAccent</item>

    </style>



    <style name="AppTheme_skyblue" parent="Theme.AppCompat.Light.NoActionBar">

        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/skyblue_colorPrimary</item>

        <item name="colorPrimaryDark">@color/skyblue_colorPrimaryDark</item>

        <item name="colorAccent">@color/skyblue_colorAccent</item>

    </style>



    <style name="AppTheme_green" parent="Theme.AppCompat.Light.NoActionBar">

        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/green_colorPrimary</item>

        <item name="colorPrimaryDark">@color/green_colorPrimaryDark</item>

        <item name="colorAccent">@color/green_colorAccent</item>

    </style>



    <style name="AppTheme_grey" parent="Theme.AppCompat.Light.NoActionBar">

        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/grey_colorPrimary</item>

        <item name="colorPrimaryDark">@color/grey_colorPrimaryDark</item>

        <item name="colorAccent">@color/grey_colorAccent</item>

    </style>



    <style name="AppTheme_brown" parent="Theme.AppCompat.Light.NoActionBar">

        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/brown_colorPrimary</item>

        <item name="colorPrimaryDark">@color/brown_colorPrimaryDark</item>

        <item name="colorAccent">@color/brown_colorAccent</item>

    </style>



    <color name="red_colorPrimary">#ff0000</color>

    <color name="red_colorPrimaryDark">#DF0000</color>

    <color name="red_colorAccent">#F7F72D</color>



    <color name="pink_colorPrimary">#e91e63</color>

    <color name="pink_colorPrimaryDark">#CB1755</color>

    <color name="pink_colorAccent">#2095f2</color>



    <color name="darkpink_colorPrimary">#9b26af</color>

    <color name="darkpink_colorPrimaryDark">#7a1ea1</color>

    <color name="darkpink_colorAccent">#f34235</color>



    <color name="violet_colorPrimary">#6639b6</color>

    <color name="violet_colorPrimaryDark">#502ca7</color>

    <color name="violet_colorAccent">#e81d62</color>



    <color name="blue_colorPrimary">#3F51B5</color>

    <color name="blue_colorPrimaryDark">#3445A2</color>

    <color name="blue_colorAccent">#e81d62</color>



    <color name="skyblue_colorPrimary">#03A9F4</color>

    <color name="skyblue_colorPrimaryDark">#0094D7</color>

    <color name="skyblue_colorAccent">#fec006</color>



    <color name="green_colorPrimary">#4CAF50</color>

    <color name="green_colorPrimaryDark">#419744</color>

    <color name="green_colorAccent">#785447</color>



    <!--<color name="colorPrimary">#3F51B5</color>-->

    <!--<color name="colorPrimaryDark">#3445A2</color>-->



    <color name="grey_colorPrimary">#9d9d9d</color>

    <color name="grey_colorPrimaryDark">#606060</color>

    <color name="grey_colorAccent">#f90</color>



    <color name="brown_colorPrimary">#795548</color>

    <color name="brown_colorPrimaryDark">#63453B</color>

    <color name="brown_colorAccent">#e81d62</color>



    <style name="AppTheme.NoActionBar">

        <item name="windowActionBar">false</item>

        <item name="windowNoTitle">true</item>

    </style>



    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />



</resources>

6. buatlah folder menu kemudian buat menu_catalog.xml
<?xml version="1.0" encoding="utf-8"?>

<menu

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    tools:context=".EmployeeActivity">



    <item

        android:id="@+id/action_search"

        android:icon="@drawable/ic_search"

        app:showAsAction="always"

        android:title="Search"

        app:actionViewClass="android.support.v7.widget.SearchView" />

    <item

        android:id="@+id/delete_all"

        app:showAsAction="never"

        android:title="Delete All Employees" />



    <item

        android:id="@+id/setting"

        app:showAsAction="never"

        android:title="Setting" />

</menu>

Selanjutnya buat lagi menu_editor dan isikan dua item untuk save dan delete
<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:android="http://schemas.android.com/apk/res/android">



    <item

        android:id="@+id/action_save"

        android:icon="@drawable/ic_check"

        app:showAsAction="always"

        android:title="add" />

    <item

        android:id="@+id/action_delete"

        android:icon="@drawable/ic_delete_forever"

        app:showAsAction="always"

        android:title="delete" />

</menu>

7. buat folder xml di res dan buat file searchable.xml dengan isi sebagai berikut
<?xml version="1.0" encoding="utf-8"?>

<searchable xmlns:android="http://schemas.android.com/apk/res/android"

    android:label="EmployeeDirectory"

    android:hint="Search Employees"

    android:searchSuggestAuthority="com.giviews.employee"

    android:searchSuggestIntentAction="android.intent.action.VIEW"

    android:searchSuggestIntentData="content://com.giviews.employee.data.EmployeeContract/employees-path"

    android:searchSuggestSelection="first_name LIKE ? OR last_name LIKE ?"

    android:searchSuggestThreshold="2"

    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" >

</searchable>

8. di folder drawable tambahkan beberapa icon, untuk menambahkan icon pada android studio bisa dilihat pada halaman ini

cukup sudah persiapan kita selanjutnya akan kita lanjutkan ke part2 kita akan membuat database helper, contract dan provider

Perintah Dasar Oracle Database

Selamat Siang, pada kesempatan kali ini admin akan membahas perintah-perintah dasar dari DBMS Oracle, mengapa oracle? karena oracle ini mempunyai kelebihan pemrosesan data yang cepat, digunakan oleh perusahaan-perusahaan besar dan cocok untuk membuat aplikasi yang bersekala besar. dibawah ini adalah rangkuman perintah-perintah dasar Oracle database yang dirangkum oleh admin, cocok bagi anda yang baru belajar oracle:

/***********************************************
*DDL(Data Definision Language)
**************************************************/


//----------------------koneksi--------
CONN

CONN sys as sysdba


//----------------------------Membuat user baru------------
CREATE USER selasa_diki IDENTIFIED BY stikomcki; 


//----------------KASIH PERMISION----------------
grant dba to selasa_diki;


//---------------------MELIHAT tabel-------------------------
SELECT * FROM tab;


//------------------------- melihat field dari tabel----------
desc barang;


//------------------menampilkan isi data dari tabel--------------
select * from pegawai;



//-------------------------------membuat table------------------
CREATE TABLE barang (kode_barang char(6), nama_barang varchar2(30), satuan_barang varchar2(10));

CREATE TABLE suplier (kode_suplier char(6), nama_suplier varchar2(30), alamat_suplier varchar2(50));

CREATE TABLE cutomer (kode_customer char(6), nama_customer varchar2(30), alamat_customer varchar2(50));

CREATE TABLE pasok (kode_pasok char(6), kode_barang char(6), kode_suplier char(6), jumlah_pasok number(38));

CREATE TABLE pembelian (kode_pembelian char(6), kode_barang char(6), kode_customer char(6), jumlah_pembelian number(38));



//--------menambahkan primary key---------
ALTER TABLE barang ADD CONSTRAINT pk PRIMARY KEY (kode_barang);

ALTER TABLE suplier ADD CONSTRAINT pk_suplier PRIMARY KEY (kode_suplier);

ALTER TABLE customer ADD CONSTRAINT pk_customer PRIMARY KEY (kode_customer);

ALTER TABLE pasok ADD CONSTRAINT pk_pasok PRIMARY KEY (kode_pasok);



//-----------------menambahkan foreign key--------------
ALTER TABLE pembelian ADD CONSTRAINT fk_barang FOREIGN KEY (kode_barang) REFERENCES barang ON DELETE CASCADE;

ALTER TABLE pembelian ADD CONSTRAINT fk_customer FOREIGN KEY (kode_customer) REFERENCES customer ON DELETE CASCADE;

ALTER TABLE pasok ADD CONSTRAINT fk_brg FOREIGN KEY (kode_barang) REFERENCES barang ON DELETE CASCADE;

ALTER TABLE pasok ADD CONSTRAINT fk_suplier FOREIGN KEY (kode_suplier) REFERENCES suplier ON DELETE CASCADE;




//-----------------mengubah tipe data---------------------
ALTER TABLE suplier MODIFY (nama_suplier char(40));



//---------------menghapus table-----------
drop table pasok;



//--------------penambahan field baru------------
ALTER TABLE barang ADD (stok_barang int);







/************************************************
*DML(Data Manipulation Language)
*************************************************/

//-----------------Insert Data-----------
insert into barang(kode_barang,nama_barang,satuan_barang,stok_barang) values('B001','Lemari Es','unit',11);

insert into barang(kode_barang,nama_barang,satuan_barang,stok_barang) values('B002','Rice Cooker','buah',23);

insert into pasok values('P001','B002','S001',5);

 insert into pasok values('P002','B003','S002',20);



//-------------------UPDATE------------------------------------
UPDATE pegawai SET nama = 'BU ATIK' WHERE nama = 'ATIK';

update barang set satuan_barang = 'unit' where kode_barang = 'B003';

update pasok set kode_barang = 'B002' where kode_pasok = 'P001';
update pasok set kode_barang = 'B003' where kode_pasok = 'P002';











/******************************************************************
*Pengelompokan, pengurutan dan kriteria data
*******************************************************************



//----------GROUP BY----------------------------------------------

SELECT TUNJANGAN_ISTRI, TUNJANGAN_ANAK1, TUNJANGAN_ANAK2, MIN(TUNJANGAN_ISTRI) FROM pegawai GROUP BY TUNJANGAN_ISTRI, TUNJANGAN_ANAK1, TUNJANGAN_ANAK2;

SELECT TUNJANGAN_ISTRI, TUNJANGAN_ANAK1, TUNJANGAN_ANAK2, MAX(TUNJANGAN_ISTRI) FROM pegawai GROUP BY TUNJANGAN_ISTRI, TUNJANGAN_ANAK1, TUNJANGAN_ANAK2;

SELECT TUNJANGAN_ISTRI, TUNJANGAN_ANAK1, TUNJANGAN_ANAK2, AVG(TUNJANGAN_ISTRI) FROM pegawai GROUP BY TUNJANGAN_ISTRI, TUNJANGAN_ANAK1, TUNJANGAN_ANAK2;

SELECT TUNJANGAN_ISTRI, TUNJANGAN_ANAK1, TUNJANGAN_ANAK2, SUM(TUNJANGAN_ISTRI) FROM pegawai GROUP BY TUNJANGAN_ISTRI, TUNJANGAN_ANAK1, TUNJANGAN_ANAK2;

SELECT TUNJANGAN_ISTRI, TUNJANGAN_ANAK1, TUNJANGAN_ANAK2, COUNT(TUNJANGAN_ISTRI) FROM pegawai GROUP BY TUNJANGAN_ISTRI, TUNJANGAN_ANAK1, TUNJANGAN_ANAK2;

//------------LATIHAN---------------------------------
SELECT ID_PEGAWAI as id_peg, MIN(ID_JABATAN) as min_pasok FROM pegawai GROUP BY ID_PEGAWAI, ID-JABATAN;

SELECT ID_PEGAWAI as id_peg, MAX(ID_JABATAN) as max_pasok FROM pegawai GROUP BY ID_PEGAWAI, ID-JABATAN;

SELECT ID_PEGAWAI as id_peg, AVG(ID_JABATAN) as avg_pasok FROM pegawai GROUP BY ID_PEGAWAI, ID-JABATAN;

SELECT ID_PEGAWAI as id_peg, SUM(ID_JABATAN) as sum_pasok FROM pegawai GROUP BY ID_PEGAWAI, ID-JABATAN;

SELECT ID_PEGAWAI as id_peg, COUNT(ID_JABATAN) as count_pasok FROM pegawai GROUP BY ID_PEGAWAI, ID-JABATAN;

select kode_barang as kd_brg, min(jumlah_pasok) as minimum_pasok from pasok group by kode_barang, jumlah_pasok;

select kode_barang as kd_bg, min(jumlah_pembelian) as minimal_pembelian from pembelian group by kode_barang;

select kode_barang as kd_bg, max(jumlah_pembelian) as maximal_pembelian from pembelian group by kode_barang;

select kode_barang as kd_bg, avg(jumlah_pembelian) as rata_rata_pembelian from pembelian group by kode_barang;

select kode_barang as kd_bg, sum(jumlah_pembelian) as total_pembelian from pembelian group by kode_barang;

select kode_barang as kd_bg, count(jumlah_pembelian) as jumlah_pembelian from pembelian group by kode_barang;

//------------cacah pembelian per customer---------------
select kode_customer as kd_cust, count(jumlah_pembelian) as jumlah_pembelian from pembelian group by kode_customer;




//-------------------ORDER BY----------------------------
SELECT * FROM pegawai ORDER BY NAMA;

Download Ebook Dasar-dasar Oracle Database
bagi anda yang ingin mempelajari oracle bisa mendownload ebook pada link dibawah


ok sekianlah pembahasan oracle kali ini, semoga bermanfaat, jika ada yang kurang jelas silakan ditanyakan pada komentar dibawah, terimakasih sudah berkunjung ke blog kami