Android Studio - CRUD Menggunakan SQLite


Hello sobat, hari ini saya akan berbagi tutorial mengenai CRUD menggunakan SQLite, jadi SQLite itu adalah databases opensource yang berukuran kecil yang menggunakan fitur penuh dengan perintah SQL, jadi dalam pembuatan aplikasi ini tidak perlu menambahkan library dalam gradle karena SQLite sudah ada dalam device android, baik selanjutnya kita masuk ke langkah pembuatan.


Manifest
Masukan semua code xml pada manifest
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.codetr.tanwir.siswasqlite"> 
 
    <application 
        android:allowBackup="true" 
        android:icon="@mipmap/ic_launcher" 
        android:label="@string/app_name" 
        android:supportsRtl="true" 
        android:theme="@style/AppTheme"> 
        <activity android:name=".MainActivity"> 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
        <activity 
            android:name=".TambahSiswa" 
            android:label="@string/input"/> 
        <activity 
            android:name=".TampilSiswa" 
            android:label="@string/detail"/> 
        <activity 
            android:name=".UpdateSiswa" 
            android:label="@string/update"/> 
 
        <activity android:name=".Pencarian"> 
            <intent-filter> 
                <action android:name="android.intent.Pencarian" /> 
                <category android:name="android.intent.category.DEFAULT" /> 
            </intent-filter> 
        </activity> 
    </application> 
 
</manifest>
Design Layout
Membuat desig layout dengan struktur


  • activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ecf0f1" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity"> 
 
    <Button 
        android:id="@+id/button2" 
        style="?android:attr/borderlessButtonStyle" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentStart="true" 
        android:layout_alignParentTop="true" 
        android:drawableLeft="@drawable/icon_add" 
        android:text="@string/tambah" /> 
 
    <Button 
        android:id="@+id/btnCari" 
        style="?android:attr/borderlessButtonStyle" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_above="@+id/listView1" 
        android:layout_alignParentTop="true" 
        android:layout_toEndOf="@+id/button2" 
        android:layout_toRightOf="@+id/button2" 
        android:drawableLeft="@drawable/ic_search_black_36dp" 
        android:gravity="center_vertical" 
        android:text="@string/pencarian" /> 
 
    <ListView 
        android:id="@+id/listView1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/button2" /> 
 
</RelativeLayout>
  • activity_pencarian.xml
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:padding="10dp"> 
 
    <SearchView 
        android:id="@+id/search_view" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="@drawable/search_view_border" 
        android:iconifiedByDefault="false" 
        android:padding="2dp" 
        android:queryHint="Cari..." /> 
 
    <ListView 
        android:id="@+id/list_view" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" /> 
 
</LinearLayout>
  • activity_tambah_siswa.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".TambahSiswa"> 
 
    <EditText 
        android:id="@+id/editText1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/textView1" 
        android:layout_below="@+id/textView1"> 
 
        <requestFocus /> 
    </EditText> 
 
    <TextView 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true" 
        android:text="NIS" /> 
 
    <TextView 
        android:id="@+id/textView2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/editText1" 
        android:layout_below="@+id/editText1" 
        android:layout_marginTop="10dp" 
        android:text="Nama" /> 
 
    <EditText 
        android:id="@+id/editText2" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/textView2" 
        android:layout_below="@+id/textView2" /> 
 
    <Button 
        android:id="@+id/button2" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentStart="true" 
        android:layout_below="@+id/button1" 
        android:text="Kembali" /> 
 
    <Button 
        android:id="@+id/button1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentEnd="true" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentStart="true" 
        android:layout_below="@+id/editText2" 
        android:layout_marginTop="12dp" 
        android:text="Simpan" /> 
 
</RelativeLayout>
  • activity_tampil_siswa.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".TampilSiswa"> 
 
    <TextView 
        android:id="@+id/textView1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentEnd="true" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentTop="true" 
        android:layout_marginLeft="70dp" 
        android:layout_marginTop="20dp" 
        android:text="TextView" /> 
 
    <TextView 
        android:id="@+id/textView2" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignRight="@+id/textView1" 
        android:layout_below="@+id/textView1" 
        android:layout_marginLeft="70dp" 
        android:layout_marginTop="20dp" 
        android:text="TextView" /> 
 
    <TextView 
        android:id="@+id/TextView02" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignBaseline="@+id/textView2" 
        android:layout_alignBottom="@+id/textView2" 
        android:layout_alignLeft="@+id/TextView01" 
        android:text="Nama" /> 
 
    <TextView 
        android:id="@+id/TextView01" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_above="@+id/textView2" 
        android:layout_alignParentLeft="true" 
        android:text="NIS" /> 
 
    <Button 
        android:id="@+id/button1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentStart="true" 
        android:layout_below="@+id/TextView02" 
        android:layout_marginTop="38dp" 
        android:text="Kembali" /> 
 
</RelativeLayout>
  • activity_update_siswa.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".UpdateSiswa"> 
 
    <EditText 
        android:id="@+id/editText1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/textView1" 
        android:layout_below="@+id/textView1" 
        android:enabled="false" /> 
 
    <TextView 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentTop="true" 
        android:text="Nis" /> 
 
    <TextView 
        android:id="@+id/textView2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/editText1" 
        android:layout_below="@+id/editText1" 
        android:layout_marginTop="10dp" 
        android:text="Nama" /> 
 
    <EditText 
        android:id="@+id/editText2" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/textView2" 
        android:layout_below="@+id/textView2"> 
 
        <requestFocus /> 
    </EditText> 
 
    <Button 
        android:id="@+id/button1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentStart="true" 
        android:layout_below="@+id/editText2" 
        android:layout_marginTop="20dp" 
        android:text="Update" /> 
 
    <Button 
        android:id="@+id/button2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentEnd="true" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentStart="true" 
        android:layout_below="@+id/button1" 
        android:text="Back" /> 
 
</RelativeLayout>
  • list_items.xml
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:padding="10dp"> 
 
    <TextView 
        android:id="@+id/name" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:padding="5dp" 
        android:textColor="#000000" 
        android:textSize="18dp" /> 
 
</LinearLayout>
Java Class
Membuat class dengan struktur



  • DataHelper.java
package com.codetr.tanwir.siswasqlite; 
 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 
 
/** 
 * Created by tanwir on 06/06/2016. 
 */ 
public class DataHelper extends SQLiteOpenHelper { 
 
    private static final String DATABASE_NAME = "dbsiswa.db"; 
    private static final int DATABASE_VERSION = 1; 
 
    public DataHelper(Context context) { 
        super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 
 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
        String sql = "create table tbsiswa (nis integer primary key,nama text null);"; 
        Log.d("Data", "onCreate: " + sql); 
        db.execSQL(sql); 
        sql = "INSERT INTO tbsiswa (nis, nama) VALUES " + 
                "('1111', 'TANWIR');"; 
        db.execSQL(sql); 
    } 
 
    @Override 
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { 
    } 
}
  • MainActivity.java
package com.codetr.tanwir.siswasqlite; 
 
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.MenuInflater; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.SearchView; 
import android.widget.Toast; 
 
/** 
 * Created by tanwir on 06/06/2016. 
 */ 
public class MainActivity extends AppCompatActivity { 
 
    private String[] daftar; 
    public ListView ListView01; 
    protected Cursor cursor; 
    private DataHelper dbcenter; 
    public static MainActivity ma; 
    public Button Cari; 
    public ListView lv; 
    public ArrayAdapter<String> adapter; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        lv = (ListView) findViewById(R.id.listView1); 
        Button btn = (Button) findViewById(R.id.button2); 
        btn.setOnClickListener(new View.OnClickListener() { 
 
            @Override 
            public void onClick(View arg0) { 
                Intent inte = new Intent(MainActivity.this, TambahSiswa.class); 
                startActivity(inte); 
            } 
        }); 
        ma = this; 
        dbcenter = new DataHelper(this); 
        Cari = (Button) findViewById(R.id.btnCari); 
        Cari.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                Intent i = new Intent("android.intent.Pencarian"); 
                startActivity(i); 
            } 
        }); 
 
 
        dbcenter = new DataHelper(this); 
        SQLiteDatabase db = dbcenter.getReadableDatabase(); 
        cursor = db.rawQuery("SELECT * FROM tbsiswa", null); 
        daftar = new String[cursor.getCount()]; 
        cursor.moveToFirst(); 
        for (int cc = 0; cc < cursor.getCount(); cc++) { 
            cursor.moveToPosition(cc); 
            daftar[cc] = cursor.getString(0).toString(); 
        } 
 
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, daftar); 
        lv.setAdapter(adapter); 
        lv.setSelected(true); 
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
 
            public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { 
 
                final String selection = daftar[arg2]; 
                final CharSequence[] dialogitem = {"Lihat Detail", "Update", "Hapus"}; 
                android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(MainActivity.this); 
                builder.setTitle("Pilihan"); 
                builder.setItems(dialogitem, new DialogInterface.OnClickListener() { 
                    public void onClick(DialogInterface dialog, int item) { 
                        switch (item) { 
                            case 0: 
                                Intent i = new Intent(getApplicationContext(), TampilSiswa.class); 
                                i.putExtra("nis", selection); 
                                startActivity(i); 
                                break; 
                            case 1: 
                                Intent in = new Intent(getApplicationContext(), UpdateSiswa.class); 
                                in.putExtra("nis", selection); 
                                startActivity(in); 
                                break; 
                            case 2: 
                                SQLiteDatabase db = dbcenter.getWritableDatabase(); 
                                db.execSQL("delete from tbsiswa where nis = '" + selection + "'"); 
                                RefreshList(); 
                                Toast.makeText(MainActivity.this, "Berhasil dihapus", Toast.LENGTH_SHORT).show(); 
                                break; 
                        } 
                    } 
                }); 
                builder.create().show(); 
            } 
        }); 
 
        ((ArrayAdapter) lv.getAdapter()).notifyDataSetInvalidated(); 
    } 
 
    public void RefreshList() { 
        SQLiteDatabase db = dbcenter.getReadableDatabase(); 
        cursor = db.rawQuery("SELECT * FROM tbsiswa", null); 
        daftar = new String[cursor.getCount()]; 
        cursor.moveToFirst(); 
        for (int cc = 0; cc < cursor.getCount(); cc++) { 
            cursor.moveToPosition(cc); 
            daftar[cc] = cursor.getString(0).toString(); 
        } 
        ListView01 = (ListView) findViewById(R.id.listView1); 
        ListView01.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, daftar)); 
        ListView01.setSelected(true); 
        ((ArrayAdapter) ListView01.getAdapter()).notifyDataSetInvalidated(); 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        MenuInflater inflater = getMenuInflater(); 
        inflater.inflate(R.menu.main, menu); 
        return true; 
    } 
}
  • Pencarian.java
package com.codetr.tanwir.siswasqlite; 
 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.SearchView; 
 
/** 
 * Created by tanwir on 06/06/2016. 
 */ 
public class Pencarian extends AppCompatActivity implements 
        SearchView.OnQueryTextListener { 
 
    private String[] daftar; 
    protected Cursor cursor; 
    private DataHelper dbcenter; 
    public ListView lv; 
    public SearchView search_view; 
    private ArrayAdapter<String> adapter; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_pencarian); 
 
        lv = (ListView) findViewById(R.id.list_view); 
        search_view = (SearchView) findViewById(R.id.search_view); 
 
        dbcenter = new DataHelper(this); 
        SQLiteDatabase db = dbcenter.getReadableDatabase(); 
        cursor = db.rawQuery("SELECT * FROM tbsiswa", null); 
        daftar = new String[cursor.getCount()]; 
        cursor.moveToFirst(); 
        for (int cc = 0; cc < cursor.getCount(); cc++) { 
            cursor.moveToPosition(cc); 
            daftar[cc] = cursor.getString(0).toString(); 
        } 
 
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, daftar); 
        lv.setAdapter(adapter); 
        search_view.setOnQueryTextListener(this); 
        lv.setSelected(true); 
        ((ArrayAdapter) lv.getAdapter()).notifyDataSetInvalidated(); 
    } 
 
    @Override 
    public boolean onQueryTextChange(String newText) { 
        adapter.getFilter().filter(newText); 
        return false; 
    } 
 
    @Override 
    public boolean onQueryTextSubmit(String query) { 
        return false; 
    } 
}
  • TambahSiswa.java
package com.codetr.tanwir.siswasqlite; 
 
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.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
 
/** 
 * Created by tanwir on 06/06/2016. 
 */ 
public class TambahSiswa extends AppCompatActivity { 
    DataHelper dbHelper; 
    Button button1, button2; 
    EditText text1, text2; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_tambah_siswa); 
 
        dbHelper = new DataHelper(this); 
        text1 = (EditText) findViewById(R.id.editText1); 
        text2 = (EditText) findViewById(R.id.editText2); 
        button1 = (Button) findViewById(R.id.button1); 
        button2 = (Button) findViewById(R.id.button2); 
 
        button1.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View arg0) { 
                SQLiteDatabase db = dbHelper.getWritableDatabase(); 
                db.execSQL("insert into tbsiswa(nis, nama) values('" + 
                        text1.getText().toString() + "','" + text2.getText().toString() + "')"); 
                Toast.makeText(getApplicationContext(), "Berhasil disimpan", Toast.LENGTH_LONG).show(); 
                MainActivity.ma.RefreshList(); 
                finish(); 
            } 
        }); 
        button2.setOnClickListener(new View.OnClickListener() { 
 
            @Override 
            public void onClick(View arg0) { 
                finish(); 
            } 
        }); 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.main, menu); 
        return true; 
    } 
}
  • TampilSiswa.java
package com.codetr.tanwir.siswasqlite; 
 
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.Button; 
import android.widget.TextView; 
 
/** 
 * Created by tanwir on 06/06/2016. 
 */ 
public class TampilSiswa extends AppCompatActivity { 
 
    protected Cursor cursor; 
    public  DataHelper dbHelper; 
    public Button btnKembali; 
    public TextView text1, text2; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_tampil_siswa); 
 
        dbHelper = new DataHelper(this); 
        text1 = (TextView) findViewById(R.id.textView1); 
        text2 = (TextView) findViewById(R.id.textView2); 
 
        SQLiteDatabase db = dbHelper.getReadableDatabase(); 
        cursor = db.rawQuery("SELECT * FROM tbsiswa WHERE nis = '" + 
                getIntent().getStringExtra("nis") + "'", null); 
        cursor.moveToFirst(); 
        if (cursor.getCount() > 0) { 
            cursor.moveToPosition(0); 
            text1.setText(cursor.getString(0).toString()); 
            text2.setText(cursor.getString(1).toString()); 
 
        } 
        btnKembali = (Button) findViewById(R.id.button1); 
        btnKembali.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View arg0) { 
                finish(); 
            } 
        }); 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.main, menu); 
        return true; 
    } 
}
  • UpdateSiswa.java
package com.codetr.tanwir.siswasqlite; 
 
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.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
 
/** 
 * Created by tanwir on 06/06/2016. 
 */ 
public class UpdateSiswa extends AppCompatActivity { 
    protected Cursor cursor; 
    DataHelper dbHelper; 
    Button button1, button2; 
    EditText text1, text2; 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_update_siswa); 
 
        dbHelper = new DataHelper(this); 
        text1 = (EditText) findViewById(R.id.editText1); 
        text2 = (EditText) findViewById(R.id.editText2); 
        SQLiteDatabase db = dbHelper.getReadableDatabase(); 
        cursor = db.rawQuery("SELECT * FROM tbsiswa WHERE nis = '" + 
                getIntent().getStringExtra("nis") + "'", null); 
        cursor.moveToFirst(); 
        if (cursor.getCount() > 0) { 
            cursor.moveToPosition(0); 
            text1.setText(cursor.getString(0).toString()); 
            text2.setText(cursor.getString(1).toString()); 
        } 
        button1 = (Button) findViewById(R.id.button1); 
        button2 = (Button) findViewById(R.id.button2); 
 
        button1.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View arg0) { 
                SQLiteDatabase db = dbHelper.getWritableDatabase(); 
                db.execSQL("update tbsiswa set nama='" + text2.getText().toString() + "' where nis='" + text1.getText().toString() + "'"); 
                Toast.makeText(getApplicationContext(), "Berhasil diupdate", Toast.LENGTH_LONG).show(); 
                MainActivity.ma.RefreshList(); 
                finish(); 
            } 
        }); 
        button2.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View arg0) { 
                finish(); 
            } 
        }); 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.main, menu); 
        return true; 
    } 
}

Vidio Tutorial
Jika kurang jelas lihat vidio berikut


Output Aplikasi



Sekian artikel dari saya semoga bermanfaat, jika ada yang ditanyakan comment saja di bawah.

Comments

Post a Comment

Popular posts from this blog

PRIVACY POLICY - APLICATION BELMANDIYSAH

PRIVACY POLICY BELMANDIYSAH

Menampilkan Data Menggunakan RecyclerView (Retrofit) Part 1