Aplikasi CRUD Sederhana Menggunakan Android Studio dengan Database SQLite

Posted by


Sebelumnya mungkin ada yang sudah buat CRUD java dengan Database mysql , nah saya akan membuat aplikasi CRUD ( Create ,Read ,Update, dan Delete ) juga tetapi dengan Android Studio yang berbasis mobile dengan database SQLite , di sini saya menggunakan Android Studio 2.1.2 , oke mulai saja .
  1. Buka Android Studio
  2. Buat Projek Baru
  3. Berinama Projek yang akan kita buat
  4. kita harus menentukan Target Android Device nya , saya pilih API 15.
  5. Pilih Empty Activity
  6. Selanjutnya pada bagian Customize The Activity biarkan saja default
  7. Setelah ke 6 langkah tadi selesai kita buat DataHelper.java , DataHelper.java ini adalah proses pembuatan Database SQLiteSimpan dengan Nama DataHelper.java
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;
    
    public class DataHelper extends SQLiteOpenHelper {
    
        private static final String DATABASE_NAME = "biodatadiri.db";
        private static final int DATABASE_VERSION = 1;
        public DataHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            String sql = "create table biodata(no integer primary key, nama text null, tgl text null, jk text null, alamat text null);";
            Log.d("Data", "onCreate: " + sql);
            db.execSQL(sql);
            sql = "INSERT INTO biodata (no, nama, tgl, jk, alamat) VALUES ('1', 'Darsiwan', '1996-07-12', 'Laki-laki','Indramayu');";
            db.execSQL(sql);
        }
        @Override
        public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
            // TODO Auto-generated method stub
        }
    }
  8. Kita buat layout xml pada activity_main.xml activity_main.xml
  9. kita tuliskan source di bawah pada MainActivity.java  
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ListView;
    
    public class MainActivity extends AppCompatActivity {
        String[] daftar;
        ListView ListView01;
        Menu menu;
        protected Cursor cursor;
        DataHelper dbcenter;
        public static MainActivity ma;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button btn=(Button)findViewById(R.id.button2);
    
            btn.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent inte = new Intent(MainActivity.this, BuatBiodata.class);
                    startActivity(inte);
                }
            });
    
            ma = this;
            dbcenter = new DataHelper(this);
            RefreshList();
        }
    
        public void RefreshList(){
            SQLiteDatabase db = dbcenter.getReadableDatabase();
            cursor = db.rawQuery("SELECT * FROM biodata",null);
            daftar = new String[cursor.getCount()];
            cursor.moveToFirst();
            for (int cc=0; cc 
    
  10. Buat 3 Empty Activity baru screenshot (1) Berinama Layout Name sebagai berikut : 1.activity_buat_biodata.xml 2.activity_lihat_biodata.xml 3.activity_update_biodata.xml Dan Activity Name : 1.BuatBiodata.java 2.LihatBiodata.java 3.UpdateBiodata.java
  11. Setelah ke 3 Activity tadi di buat tuliskan pada layout xml dengan nama masing masing : 1.activity_buat_biodata.xml 2.activity_lihat_biodata.xml 3.activity_update_biodata.xml
  12. Tuliskan baris kode java class di bawah dengan nama masing-masing : 1.BuatBiodata.java
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class BuatBiodata extends AppCompatActivity {
        protected Cursor cursor;
        DataHelper dbHelper;
        Button ton1, ton2;
        EditText text1, text2, text3, text4, text5;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_buat_biodata);
    
            dbHelper = new DataHelper(this);
            text1 = (EditText) findViewById(R.id.editText1);
            text2 = (EditText) findViewById(R.id.editText2);
            text3 = (EditText) findViewById(R.id.editText3);
            text4 = (EditText) findViewById(R.id.editText4);
            text5 = (EditText) findViewById(R.id.editText5);
            ton1 = (Button) findViewById(R.id.button1);
            ton2 = (Button) findViewById(R.id.button2);
    
            ton1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    db.execSQL("insert into biodata(no, nama, tgl, jk, alamat) values('" +
                            text1.getText().toString() + "','" +
                            text2.getText().toString() + "','" +
                            text3.getText().toString() + "','" +
                            text4.getText().toString() + "','" +
                            text5.getText().toString() + "')");
                    Toast.makeText(getApplicationContext(), "Berhasil", Toast.LENGTH_LONG).show();
                    MainActivity.ma.RefreshList();
                    finish();
                }
            });
            ton2.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    finish();
                }
            });
        }
    
    }
    2.LihatBiodata.java
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class LihatBiodata extends AppCompatActivity {
        protected Cursor cursor;
        DataHelper dbHelper;
        Button ton2;
        TextView text1, text2, text3, text4, text5;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_lihat_biodata);
    
            dbHelper = new DataHelper(this);
            text1 = (TextView) findViewById(R.id.textView1);
            text2 = (TextView) findViewById(R.id.textView2);
            text3 = (TextView) findViewById(R.id.textView3);
            text4 = (TextView) findViewById(R.id.textView4);
            text5 = (TextView) findViewById(R.id.textView5);
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            cursor = db.rawQuery("SELECT * FROM biodata WHERE nama = '" +
                    getIntent().getStringExtra("nama") + "'",null);
            cursor.moveToFirst();
            if (cursor.getCount()>0)
            {
                cursor.moveToPosition(0);
                text1.setText(cursor.getString(0).toString());
                text2.setText(cursor.getString(1).toString());
                text3.setText(cursor.getString(2).toString());
                text4.setText(cursor.getString(3).toString());
                text5.setText(cursor.getString(4).toString());
            }
            ton2 = (Button) findViewById(R.id.button1);
            ton2.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    finish();
                }
            });
        }
    
    }
    3.UpdateBiodata.java
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class UpdateBiodata extends AppCompatActivity {
        protected Cursor cursor;
        DataHelper dbHelper;
        Button ton1, ton2;
        EditText text1, text2, text3, text4, text5;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_update_biodata);
    
            dbHelper = new DataHelper(this);
            text1 = (EditText) findViewById(R.id.editText1);
            text2 = (EditText) findViewById(R.id.editText2);
            text3 = (EditText) findViewById(R.id.editText3);
            text4 = (EditText) findViewById(R.id.editText4);
            text5 = (EditText) findViewById(R.id.editText5);
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            cursor = db.rawQuery("SELECT * FROM biodata WHERE nama = '" +
                    getIntent().getStringExtra("nama") + "'",null);
            cursor.moveToFirst();
            if (cursor.getCount()>0)
            {
                cursor.moveToPosition(0);
                text1.setText(cursor.getString(0).toString());
                text2.setText(cursor.getString(1).toString());
                text3.setText(cursor.getString(2).toString());
                text4.setText(cursor.getString(3).toString());
                text5.setText(cursor.getString(4).toString());
            }
            ton1 = (Button) findViewById(R.id.button1);
            ton2 = (Button) findViewById(R.id.button2);
            // daftarkan even onClick pada btnSimpan
            ton1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    db.execSQL("update biodata set nama='"+
                            text2.getText().toString() +"', tgl='" +
                            text3.getText().toString()+"', jk='"+
                            text4.getText().toString() +"', alamat='" +
                            text5.getText().toString() + "' where no='" +
                            text1.getText().toString()+"'");
                    Toast.makeText(getApplicationContext(), "Berhasil", Toast.LENGTH_LONG).show();
                    MainActivity.ma.RefreshList();
                    finish();
                }
            });
            ton2.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    finish();
                }
            });
        }
    
    }

    Lanjutan..

  13. Tambahan : edit manifest/AndroidManifest.xml edit res/values/color.xml Terakhir edit res/values/styles.xml
  14. Jalankan aplikasinya ..

Update

Untuk pembaca yang mengalami error :
Error:(13, 32) No resource found that matches the given name (at 'paddingBottom' with value '@dimen/activity_vertical_margin').`
Tambahkan kode berikut di /res/values/dimens.xml::

<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>

Penutup

Kurang lebihnya mohon maaf bila ada yang kurang jelas tanyakan pada kolom komentar . terima kasih semoga bermanfaat !
Aplikasi jadi, bisa di download di sini .


Blog, Updated at: September 05, 2019

0 comments:

Post a Comment

Terimakasih

Labels

Labels

!doctype (1) .net (1) 2008 (1) 2012 (1) 2013 (3) 3d (2) 3ds max (2) abstrak (1) access (3) adobe (1) apache (1) array (2) asp (7) asp.net (3) autodesk (1) biodata (2) biodata asp (2) biodata php (1) blender (1) c# (1) c3 (1) case (1) client (1) cmd (1) coding (24) combobox (1) cs6 (1) database (3) databse (1) dhcp (1) dreamweaver (2) echo (1) Encapsilation (1) Enkapsulasi (1) field (1) footer (1) form (5) form asp (1) gabung (1) gitar (1) gns3 (1) guitar (1) header (1) html (2) include (1) inheritance (1) input (1) install (1) ip (2) jaringan (4) java (1) komponen komponen validator (1) label (1) lan (1) localhost (2) manage sites (1) management (1) membuat (2) membuat biodata array php (1) membuat biodata satu variable (1) menghubukan php dengan database (1) microsoft (2) mikrotik (3) mikrotik tanpa router (1) mikrotik virtual box (2) ms access (3) multimedia (4) music (1) musik (1) mysql (2) netbeans (1) netbeans 7.3.1 (1) new site (1) office (2) oracle (1) osi layer (1) panelcontrol (1) petik (1) pewarisan java (1) php (14) php designer (1) php4 (1) polymorphic (1) polymorphism (1) Poymorphisme (1) primary key (1) radiobutton (1) range (1) require (1) required (1) routeros (2) script (1) server (2) shredder (1) sql server (1) summary (1) swift (1) table (1) tanda petik (1) textbox (1) toolbar (1) validasi (1) validation (1) validator (1) variable (2) vb (1) visual studio 2012 ultimate (4) volume asp (1) web (1) web server (2) win 7 (1) win xp (1) windowd form (1) windows (1) XAMPP (2) xp (1)