BELAJAR TAMPILAN ANDROID MYSQL DAN PHP PART 01

  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
    }
}
Kita buat layout xml pada activity_main.xml activity_main.xml
  1. DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2.                xmlns:tools="http://schemas.android.com/tools"
    3.                android:layout_width="match_parent"
    4.                android:layout_height="match_parent"
    5.                android:paddingBottom="@dimen/activity_vertical_margin"
    6.                android:paddingLeft="@dimen/activity_horizontal_margin"
    7.                android:paddingRight="@dimen/activity_horizontal_margin"
    8.                android:paddingTop="@dimen/activity_vertical_margin"
    9.                android:background="#ecf0f1"
    10.                tools:context=".MainActivity" >
    11.     <ListView
    12.        android:id="@+id/listView1"
    13.        android:layout_width="match_parent"
    14.        android:layout_height="wrap_content"
    15.        android:layout_alignParentTop="true"
    16.        android:layout_alignParentRight="true"
    17.        android:layout_alignParentEnd="true"
    18.        android:layout_above="@+id/button2">
    19.     </ListView>
    20.     <Button
    21.        android:id="@+id/button2"
    22.        android:layout_width="wrap_content"
    23.        android:layout_height="wrap_content"
    24.        style="?android:attr/borderlessButtonStyle"
    25.        android:drawableLeft="@drawable/icon_add"
    26.        android:text="Buat Biodata Baru"
    27.        android:layout_alignParentBottom="true"
    28.        android:layout_alignParentLeft="true"
    29.        android:layout_alignParentStart="true" />
    30. </RelativeLayout>
  2. 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 < cursor.getCount(); cc++){
                cursor.moveToPosition(cc);
                daftar[cc] = cursor.getString(1).toString();
            }
            ListView01 = (ListView)findViewById(R.id.listView1);
            ListView01.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, daftar));
            ListView01.setSelected(true);
            ListView01.setOnItemClickListener(new OnItemClickListener() {
    
    
                public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
                    final String selection = daftar[arg2]; //.getItemAtPosition(arg2).toString();
                    final CharSequence[] dialogitem = {"Lihat Biodata", "Update Biodata", "Hapus Biodata"};
                    AlertDialog.Builder builder = new 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(), LihatBiodata.class);
                                    i.putExtra("nama", selection);
                                    startActivity(i);
                                    break;
                                case 1 :
                                    Intent in = new Intent(getApplicationContext(), UpdateBiodata.class);
                                    in.putExtra("nama", selection);
                                    startActivity(in);
                                    break;
                                case 2 :
                                    SQLiteDatabase db = dbcenter.getWritableDatabase();
                                    db.execSQL("delete from biodata where nama = '"+selection+"'");
                                    RefreshList();
                                    break;
                            }
                        }
                    });
                    builder.create().show();
                }});
            ((ArrayAdapter)ListView01.getAdapter()).notifyDataSetInvalidated();
        }
    
    Empty Activity baru
screenshot (1)Berinama Layout Name sebagai berikut : 1.activity_buat_biodata.xml 
  1. 2.activity_lihat_biodata.xml 3.activity_update_biodata.xml
    Dan Activity Name :
    1.BuatBiodata.java 2.LihatBiodata.java 3.UpdateBiodata.java
  2. Setelah ke 3 Activity tadi di buat tuliskan pada layout xml dengan nama masing masing : 1.activity_buat_biodata.xml
    DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2.                xmlns:tools="http://schemas.android.com/tools"
    3.                android:layout_width="match_parent"
    4.                android:layout_height="match_parent"
    5.                android:paddingBottom="@dimen/activity_vertical_margin"
    6.                android:paddingLeft="@dimen/activity_horizontal_margin"
    7.                android:paddingRight="@dimen/activity_horizontal_margin"
    8.                android:paddingTop="@dimen/activity_vertical_margin"
    9.                tools:context=".BuatBiodata" >
    10.     <EditText
    11.        android:id="@+id/editText1"
    12.        android:layout_width="match_parent"
    13.        android:layout_height="wrap_content"
    14.        android:layout_alignLeft="@+id/textView1"
    15.        android:layout_below="@+id/textView1"
    16.        android:inputType="number"
    17.        android:maxLength="10">
    18.         <requestFocus />
    19.     </EditText>
    20.     <TextView
    21.        android:id="@+id/textView1"
    22.        android:layout_width="wrap_content"
    23.        android:layout_height="wrap_content"
    24.        android:layout_alignParentLeft="true"
    25.        android:layout_alignParentTop="true"
    26.        android:text="Nomor" />
    27.     <TextView
    28.        android:id="@+id/textView2"
    29.        android:layout_width="wrap_content"
    30.        android:layout_height="wrap_content"
    31.        android:layout_alignLeft="@+id/editText1"
    32.        android:layout_below="@+id/editText1"
    33.        android:layout_marginTop="10dp"
    34.        android:text="Nama" />
    35.     <EditText
    36.        android:id="@+id/editText2"
    37.        android:layout_width="match_parent"
    38.        android:layout_height="wrap_content"
    39.        android:layout_alignLeft="@+id/textView2"
    40.        android:layout_below="@+id/textView2"
    41.        android:inputType="text"
    42.        android:maxLength="20"/>
    43.     <TextView
    44.        android:id="@+id/textView3"
    45.        android:layout_width="wrap_content"
    46.        android:layout_height="wrap_content"
    47.        android:layout_alignLeft="@+id/editText2"
    48.        android:layout_below="@+id/editText2"
    49.        android:layout_marginTop="10dp"
    50.        android:text="Tanggal Lahir" />
    51.     <EditText
    52.        android:id="@+id/editText3"
    53.        android:layout_width="match_parent"
    54.        android:layout_height="wrap_content"
    55.        android:layout_alignLeft="@+id/textView3"
    56.        android:layout_below="@+id/textView3"
    57.        android:inputType="date"/>
    58.     <TextView
    59.        android:id="@+id/textView4"
    60.        android:layout_width="wrap_content"
    61.        android:layout_height="wrap_content"
    62.        android:layout_alignLeft="@+id/editText3"
    63.        android:layout_below="@+id/editText3"
    64.        android:layout_marginTop="10dp"
    65.        android:text="Jenis Kelamin" />
    66.     <EditText
    67.        android:id="@+id/editText4"
    68.        android:layout_width="match_parent"
    69.        android:layout_height="wrap_content"
    70.        android:layout_alignLeft="@+id/textView4"
    71.        android:layout_below="@+id/textView4"
    72.        android:inputType="text"
    73.        android:maxLength="1"
    74.        android:hint="L atau P"/>
    75.     <TextView
    76.        android:id="@+id/textView5"
    77.        android:layout_width="wrap_content"
    78.        android:layout_height="wrap_content"
    79.        android:layout_alignLeft="@+id/editText4"
    80.        android:layout_below="@+id/editText4"
    81.        android:layout_marginTop="10dp"
    82.        android:text="Alamat" />
    83.     <EditText
    84.        android:id="@+id/editText5"
    85.        android:layout_width="match_parent"
    86.        android:layout_height="wrap_content"
    87.        android:layout_alignLeft="@+id/textView5"
    88.        android:layout_below="@+id/textView5"
    89.        android:maxLength="100"
    90.        android:inputType="text"/>
    91.     <Button
    92.        android:id="@+id/button1"
    93.        android:layout_width="wrap_content"
    94.        android:layout_height="wrap_content"
    95.        android:layout_alignLeft="@+id/editText5"
    96.        android:layout_alignParentBottom="true"
    97.        style="?android:attr/borderlessButtonStyle"
    98.        android:drawableLeft="@drawable/ic_done"
    99.        android:text="Simpan" />
    100.     <Button
    101.        android:id="@+id/button2"
    102.        android:layout_width="wrap_content"
    103.        android:layout_height="wrap_content"
    104.        android:text="Kembali"
    105.        style="?android:attr/borderlessButtonStyle"
    106.        android:drawableLeft="@drawable/ic_arrow"
    107.        android:layout_alignParentBottom="true"
    108.        android:layout_alignRight="@+id/editText5"
    109.        android:layout_alignEnd="@+id/editText5" />
    110. </RelativeLayout>
    2.activity_lihat_biodata.xml
    DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2.                xmlns:tools="http://schemas.android.com/tools"
    3.                android:layout_width="match_parent"
    4.                android:layout_height="match_parent"
    5.                android:paddingBottom="@dimen/activity_vertical_margin"
    6.                android:paddingLeft="@dimen/activity_horizontal_margin"
    7.                android:paddingRight="@dimen/activity_horizontal_margin"
    8.                android:paddingTop="@dimen/activity_vertical_margin"
    9.                tools:context=".LihatBiodata" >
    10.     <TextView
    11.        android:id="@+id/textView1"
    12.        android:layout_width="wrap_content"
    13.        android:layout_height="wrap_content"
    14.        android:layout_alignParentRight="true"
    15.        android:layout_alignParentTop="true"
    16.        android:layout_marginRight="104dp"
    17.        android:layout_marginTop="20dp"
    18.        android:text="TextView" />
    19.     <TextView
    20.        android:id="@+id/textView2"
    21.        android:layout_width="wrap_content"
    22.        android:layout_height="wrap_content"
    23.        android:layout_alignRight="@+id/textView1"
    24.        android:layout_below="@+id/textView1"
    25.        android:layout_marginTop="20dp"
    26.        android:text="TextView" />
    27.     <TextView
    28.        android:id="@+id/textView3"
    29.        android:layout_width="wrap_content"
    30.        android:layout_height="wrap_content"
    31.        android:layout_alignLeft="@+id/textView2"
    32.        android:layout_below="@+id/textView2"
    33.        android:layout_marginTop="20dp"
    34.        android:text="TextView" />
    35.     <TextView
    36.        android:id="@+id/textView4"
    37.        android:layout_width="wrap_content"
    38.        android:layout_height="wrap_content"
    39.        android:layout_alignLeft="@+id/textView3"
    40.        android:layout_below="@+id/textView3"
    41.        android:layout_marginTop="20dp"
    42.        android:text="TextView" />
    43.     <TextView
    44.        android:id="@+id/textView5"
    45.        android:layout_width="wrap_content"
    46.        android:layout_height="wrap_content"
    47.        android:layout_alignRight="@+id/textView4"
    48.        android:layout_below="@+id/textView4"
    49.        android:layout_marginTop="20dp"
    50.        android:text="TextView" />
    51.     <TextView
    52.        android:id="@+id/TextView05"
    53.        android:layout_width="wrap_content"
    54.        android:layout_height="wrap_content"
    55.        android:layout_alignBaseline="@+id/textView5"
    56.        android:layout_alignBottom="@+id/textView5"
    57.        android:layout_alignLeft="@+id/TextView03"
    58.        android:text="Alamat" />
    59.     <TextView
    60.        android:id="@+id/TextView03"
    61.        android:layout_width="wrap_content"
    62.        android:layout_height="wrap_content"
    63.        android:layout_alignBaseline="@+id/textView4"
    64.        android:layout_alignBottom="@+id/textView4"
    65.        android:layout_alignLeft="@+id/TextView04"
    66.        android:text="Jenis Kelamin" />
    67.     <TextView
    68.        android:id="@+id/TextView04"
    69.        android:layout_width="wrap_content"
    70.        android:layout_height="wrap_content"
    71.        android:layout_alignBaseline="@+id/textView3"
    72.        android:layout_alignBottom="@+id/textView3"
    73.        android:layout_alignLeft="@+id/TextView02"
    74.        android:text="Tanggal Lahir" />
    75.     <TextView
    76.        android:id="@+id/TextView02"
    77.        android:layout_width="wrap_content"
    78.        android:layout_height="wrap_content"
    79.        android:layout_alignBaseline="@+id/textView2"
    80.        android:layout_alignBottom="@+id/textView2"
    81.        android:layout_alignLeft="@+id/TextView01"
    82.        android:text="Nama" />
    83.     <TextView
    84.        android:id="@+id/TextView01"
    85.        android:layout_width="wrap_content"
    86.        android:layout_height="wrap_content"
    87.        android:layout_above="@+id/textView2"
    88.        android:layout_alignParentLeft="true"
    89.        android:text="Nomor" />
    90.     <Button
    91.        android:id="@+id/button1"
    92.        android:layout_width="wrap_content"
    93.        android:layout_height="wrap_content"
    94.        android:layout_alignLeft="@+id/TextView05"
    95.        android:layout_below="@+id/TextView05"
    96.        android:layout_marginTop="34dp"
    97.        android:text="Kembali"
    98.        style="?android:attr/borderlessButtonStyle"
    99.        android:drawableLeft="@drawable/ic_arrow"/>
    100. </RelativeLayout>
    3.activity_update_biodata.xml
    DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2.                xmlns:tools="http://schemas.android.com/tools"
    3.                android:layout_width="match_parent"
    4.                android:layout_height="match_parent"
    5.                android:paddingBottom="@dimen/activity_vertical_margin"
    6.                android:paddingLeft="@dimen/activity_horizontal_margin"
    7.                android:paddingRight="@dimen/activity_horizontal_margin"
    8.                android:paddingTop="@dimen/activity_vertical_margin"
    9.                tools:context=".UpdateBiodata" >
    10.     <EditText
    11.        android:id="@+id/editText1"
    12.        android:layout_width="match_parent"
    13.        android:layout_height="wrap_content"
    14.        android:layout_alignLeft="@+id/textView1"
    15.        android:layout_below="@+id/textView1"
    16.        android:inputType="number"
    17.        android:maxLength="10">
    18.         <requestFocus />
    19.     </EditText>
    20.     <TextView
    21.        android:id="@+id/textView1"
    22.        android:layout_width="wrap_content"
    23.        android:layout_height="wrap_content"
    24.        android:layout_alignParentLeft="true"
    25.        android:layout_alignParentTop="true"
    26.        android:text="Nomor" />
    27.     <TextView
    28.        android:id="@+id/textView2"
    29.        android:layout_width="wrap_content"
    30.        android:layout_height="wrap_content"
    31.        android:layout_alignLeft="@+id/editText1"
    32.        android:layout_below="@+id/editText1"
    33.        android:layout_marginTop="10dp"
    34.        android:text="Nama" />
    35.     <EditText
    36.        android:id="@+id/editText2"
    37.        android:layout_width="match_parent"
    38.        android:layout_height="wrap_content"
    39.        android:layout_alignLeft="@+id/textView2"
    40.        android:layout_below="@+id/textView2"
    41.        android:inputType="text"
    42.        android:maxLength="20"/>
    43.     <TextView
    44.        android:id="@+id/textView3"
    45.        android:layout_width="wrap_content"
    46.        android:layout_height="wrap_content"
    47.        android:layout_alignLeft="@+id/editText2"
    48.        android:layout_below="@+id/editText2"
    49.        android:layout_marginTop="10dp"
    50.        android:text="Tanggal Lahir" />
    51.     <EditText
    52.        android:id="@+id/editText3"
    53.        android:layout_width="match_parent"
    54.        android:layout_height="wrap_content"
    55.        android:layout_alignLeft="@+id/textView3"
    56.        android:layout_below="@+id/textView3"
    57.        android:inputType="date"/>
    58.     <TextView
    59.        android:id="@+id/textView4"
    60.        android:layout_width="wrap_content"
    61.        android:layout_height="wrap_content"
    62.        android:layout_alignLeft="@+id/editText3"
    63.        android:layout_below="@+id/editText3"
    64.        android:layout_marginTop="10dp"
    65.        android:text="Jenis Kelamin" />
    66.     <EditText
    67.        android:id="@+id/editText4"
    68.        android:layout_width="match_parent"
    69.        android:layout_height="wrap_content"
    70.        android:layout_alignLeft="@+id/textView4"
    71.        android:layout_below="@+id/textView4"
    72.        android:inputType="text"
    73.        android:maxLength="1"
    74.        android:hint="L atau P"/>
    75.     <TextView
    76.        android:id="@+id/textView5"
    77.        android:layout_width="wrap_content"
    78.        android:layout_height="wrap_content"
    79.        android:layout_alignLeft="@+id/editText4"
    80.        android:layout_below="@+id/editText4"
    81.        android:layout_marginTop="10dp"
    82.        android:text="Alamat"
    83.        android:inputType="text"
    84.        android:maxLength="100"/>
    85.     <EditText
    86.        android:id="@+id/editText5"
    87.        android:layout_width="match_parent"
    88.        android:layout_height="wrap_content"
    89.        android:layout_alignLeft="@+id/textView5"
    90.        android:layout_below="@+id/textView5" />
    91.     <Button
    92.        android:id="@+id/button1"
    93.        android:layout_width="wrap_content"
    94.        android:layout_height="wrap_content"
    95.        android:layout_alignLeft="@+id/editText5"
    96.        android:layout_alignParentBottom="true"
    97.        style="?android:attr/borderlessButtonStyle"
    98.        android:drawableLeft="@drawable/ic_done"
    99.        android:text="Update" />
    100.     <Button
    101.        android:id="@+id/button2"
    102.        android:layout_width="wrap_content"
    103.        android:layout_height="wrap_content"
    104.        android:text="Kembali"
    105.        style="?android:attr/borderlessButtonStyle"
    106.        android:drawableLeft="@drawable/ic_arrow"
    107.        android:layout_alignParentBottom="true"
    108.        android:layout_alignRight="@+id/editText5"
    109.        android:layout_alignEnd="@+id/editText5"
    110.        />
    111. </RelativeLayout>
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();
            }
        });
    }

}
Tambahan : edit manifest/AndroidManifest.xml

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.    package="com.example.darsiwan.crudsqliite">
  4.     <application
  5.        android:allowBackup="true"
  6.        android:icon="@mipmap/logo"
  7.        android:label="@string/app_name"
  8.        android:supportsRtl="true"
  9.        android:theme="@style/AppTheme">
  10.         <activity android:name=".MainActivity">
  11.             <intent-filter>
  12.                 <action android:name="android.intent.action.MAIN" />
  13.                 <category android:name="android.intent.category.LAUNCHER" />
  14.             </intent-filter>
  15.         </activity>
  16.     <activity android:name=".BuatBiodata"
  17.        android:label="@string/title_activity_buatbiodata">
  18.     </activity>
  19.     <activity android:name=".LihatBiodata"
  20.        android:label="@string/title_activity_lihatbiodata">
  21.     </activity>
  22.     <activity android:name=".UpdateBiodata"
  23.        android:label="@string/title_activity_updatebiodata">
  24.     </activity>
  25.     </application>
  26. </manifest>
edit res/values/color.xml

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <color name="colorPrimary">#3F51B5</color>
  4.     <color name="colorPrimaryDark">#303F9F</color>
  5.     <color name="colorAccent">#000</color>
  6. </resources>
Terakhir edit res/values/styles.xml

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.     <color name="colorPrimary">#3F51B5</color>
  4.     <color name="colorPrimaryDark">#303F9F</color>
  5.     <color name="colorAccent">#000</color>
  6. </resources>
Jalankan aplikasinya ..
gambar

Komentar