Beranda > Android, Pemrograman > Pemrograman Android – Menampilkan data SQL di Listview dan Penerapan CRUD

Pemrograman Android – Menampilkan data SQL di Listview dan Penerapan CRUD

30 Maret 2016

logo android studio

Pada posting sebelumnya kita sudah dapat menampilkan data SQL pada listview. Selanjutnya bagaimana menerapkan CRUD untuk memodifikasi data dalam sebuah tabel, seperti gambar dibawah ini.

data pasien 2

CRUD adalah singkatan dari Create, read, update dan delete. Pada tutorial kali ini kita akan membuat sebuah aplikasi android yang dapat menampilkan (membaca) data, menambah data baru, merubah data dan menghapus data yang terdapat dalam sebuah tabel SQL.

File yang diperlukan :

  1. MainActivity.java
  2. SQLiteDB.java
  3. CustomAdapter.java
  4. activity_main.xml
  5. list_item.xml
  6. periode_editdata.xml
  7. periode_tambahdata.xml

Berikut langkah-langkah menampilkan data SQL di Listview dan penerapan CRUD :

1 Buka Android Studio dan buat sebuah project baru.

2 Buat file SQLiteDB.java. File ini akan menangani koneksi SQLite database.


package com.wordpress.yopiardinal.listview_arrayadapter;

/**
 * Modified by Ardinal on 29/03/2016.
 *
 * Source : http://www.worldbestlearningcenter.com/tips/Android-SQLite-example.htm
 * Source : https://www.codeofaninja.com/2013/02/android-sqlite-tutorial.html
 */
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLiteDB extends SQLiteOpenHelper{


    public static final int MYDATABASE_VERSION = 1;
    public static final String MYDATABASE_NAME = "womancalendar1";
    public static final String MYDATABASE_TABLE = "tabel_period1";
    public static final String KEY_ID = "_id";
    public static final String KEY_NOTES = "notes";
    public static final String KEY_MEDICINE = "medicine";
    public static final String KEY_MOODS = "moods";

    //-------------DEKLARASI UNTUK MEMBUAT TABEL-------------//
    private static final String SCRIPT_CREATE_TABLE =
            "create table " + MYDATABASE_TABLE + " ("
                    + KEY_ID + " integer primary key autoincrement, "
                    + KEY_NOTES + " text not null, "
                    + KEY_MEDICINE + " text not null, "
                    + KEY_MOODS + " text not null);";

    //-------------DEKLARASI UNTUK MENGHAPUS TABEL-------------//
    private static final String SCRIPT_DELETE_TABLE="DROP TABLE IF EXISTS " + MYDATABASE_TABLE;


    public SQLiteDB(Context context){
        //BUAT DATABASE JIKA TIDAK ADA
        super(context,MYDATABASE_NAME,null,MYDATABASE_VERSION);
    }


    public void onCreate(SQLiteDatabase db){
        //BUAT TABEL
        db.execSQL(SCRIPT_CREATE_TABLE);
    
    }

    public void onUpgrade(SQLiteDatabase db,int olv,int newv){
        db.execSQL(SCRIPT_DELETE_TABLE);
        onCreate(db);
    }
}

3 Buat file CustomAdapter.java. File ini yang akan menangani tampilan Listview.


package com.wordpress.yopiardinal.listview_arrayadapter;

/**
 * Modified by Ardinal on 29/03/2016.
 *
 * Source : http://www.worldbestlearningcenter.com/tips/Android-SQLite-example.htm
 * Source : https://www.codeofaninja.com/2013/02/android-sqlite-tutorial.html
 */

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<String>{

    int groupid;
    ArrayList<String> records;
    Context context;

    public CustomAdapter(Context context, int vg, int id, ArrayList<String> records){
        super(context,vg, id, records);
        this.context=context;
        groupid=vg;
        this.records=records;

    }
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(groupid, parent, false);

        String[] row_items=records.get(position).split("__");
        TextView t4id= (TextView) itemView.findViewById(R.id.tempatid);
        t4id.setText("No : "+row_items[0]);
        TextView t4catatan= (TextView) itemView.findViewById(R.id.tempatcatatan);
        t4catatan.setText("Catatan : "+row_items[1]);
        TextView t4medicine= (TextView) itemView.findViewById(R.id.tempatmedicine);
        t4medicine.setText("Obat : "+row_items[2]);
        TextView t4moods= (TextView) itemView.findViewById(R.id.tempatmoods);
        t4moods.setText("Moods : "+row_items[3]);
        Button bt_del_period=(Button)itemView.findViewById(R.id.bt_delete_period);
        bt_del_period.setTag(row_items[0]);
        Button bt_edit_period=(Button)itemView.findViewById(R.id.bt_edit_period);
        bt_edit_period.setTag(row_items[0]);
        return itemView;
    }

}

4 Edit file activity_main.xml. Tampilan utama tempat menampilkan semua program.


<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"
    android:background="#fff0bf00"
    tools:context=".MainActivity"
    android:id="@+id/data_pasien">

    <ListView
        android:id="@+id/pro_lsit"
        android:layout_width="fill_parent"
        android:layout_height="550dp"
        android:layout_marginTop="40dp"

        />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Tambah Data"
        android:id="@+id/btntambahdata_Period"
        android:layout_alignParentBottom="true"
        android:onClick="tambahRow"
        android:layout_alignRight="@+id/pro_lsit"
        android:layout_alignEnd="@+id/pro_lsit" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="42dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="DATA PASIEN"
        android:id="@+id/txt_data_pasien"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textColor="#ffffffff"
        android:textStyle="bold"
        android:gravity="center"
        android:background="#ff000000" />


</RelativeLayout>

5 Buat file list_item.xml.  File yang akan membentuk sebuah layout seperti ini.

obat

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"

    android:padding="2dp"
    android:layout_height="100dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tempat Catatan"
        android:id="@+id/tempatcatatan"
        android:textColor="#ff000000"
        android:layout_below="@+id/tempatid"
        android:layout_alignParentLeft="true"

        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tempat Medicine"
        android:id="@+id/tempatmedicine"
        android:textColor="#ff000000"
        android:layout_below="@+id/tempatcatatan"
        android:layout_alignParentLeft="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tempat ID"
        android:id="@+id/tempatid"
        android:textColor="#ff000000"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tempat Moods"
        android:id="@+id/tempatmoods"

        android:phoneNumber="true"
        android:textColor="#ff000000"
        android:layout_below="@+id/tempatmedicine"
        android:layout_alignParentLeft="true" />
    <Button
        android:id="@+id/bt_delete_period"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:text="Hapus"
        android:layout_alignParentRight="true"
        android:onClick="deleteRow"

        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:text="EDIT"
        android:id="@+id/bt_edit_period"
        android:layout_below="@+id/bt_delete_period"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:onClick="editRow"/>


</RelativeLayout>

6 Buat file periode_editdata.xml. File layout yang akan dipanggil menggunakan dialog sebagai form untuk merubah data.


<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"
    >

    <EditText
        android:id="@+id/masukkanid"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:hint="Enter ID"
        android:singleLine="true"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:editable="false">
        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/masukkannotes"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:hint="Enter Notes"
        android:singleLine="true"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/masukkanmedicine"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:hint="Enter Medicine"
        android:singleLine="true"
        android:layout_below="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"/>

    <EditText
        android:id="@+id/masukkanmoods"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Moods"
        android:singleLine="true"
        android:layout_below="@+id/textView4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Masukkan ID :"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Masukkan Note :"
        android:id="@+id/textView2"
        android:layout_below="@+id/masukkanid"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Masukkan Medicine :"
        android:id="@+id/textView3"
        android:layout_below="@+id/masukkannotes"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Masukkan Moods :"
        android:id="@+id/textView4"
        android:layout_below="@+id/masukkanmedicine"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp"/>

</RelativeLayout>

7 Buat file periode_tambahdata.xml. File layout yang akan dipanggil menggunakan dialog sebagai form untuk menambah data.


<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" >

    <EditText
        android:id="@+id/masukkannotes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:hint="Enter Notes"
        android:singleLine="true" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/masukkanmedicine"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/masukkannotes"
        android:hint="Enter Medicine"
        android:singleLine="true" />

    <EditText
        android:id="@+id/masukkanmoods"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/masukkanmedicine"
        android:hint="Enter Moods"
        android:singleLine="true" />

</RelativeLayout>

8 Edit file MainActivity.java. File ini yang akan menangani semua aktivitas di program ini. Untuk mempermudah pemahaman CRUD, semua fungsi CRUD ditempatkan disini.

package com.wordpress.yopiardinal.listview_arrayadapter;

/**
 * Modified by Ardinal on 29/03/2016.
 *
 * Source : http://www.worldbestlearningcenter.com/tips/Android-SQLite-example.htm
 * Source : https://www.codeofaninja.com/2013/02/android-sqlite-tutorial.html
 */


import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;


public class MainActivity extends Activity {

    ListView listview;
    CustomAdapter adapter;
    ArrayList<String> records;
    SQLiteDB dbhelper;
    //Button btntambahdata_Period;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview=(ListView)findViewById(R.id.pro_lsit);
        records=new ArrayList<String>();
        adapter=new CustomAdapter(this,R.layout.list_item,R.id.tempatid,records);
        listview.setAdapter(adapter);
        dbhelper=new SQLiteDB(this);

    }

    public void onStart(){
        super.onStart();
        insertSampleData();
        readSampleData();
        countRecords_period();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void readSampleData(){
        //MEMBUKA DATABASE
        SQLiteDatabase database=dbhelper.getReadableDatabase();
        String sql="SELECT * FROM "+ SQLiteDB.MYDATABASE_TABLE;

        //MEMBUAT KURSOR UNTUK MEMBUKA DATABASE
        Cursor c=database.rawQuery(sql,null);
        String isicatatan, isiobat, isimoods;
        int id;
        if(c.getCount()>0)
            while(c.moveToNext()){
                id=c.getInt(c.getColumnIndex(SQLiteDB.KEY_ID));
                isicatatan=c.getString(c.getColumnIndex(SQLiteDB.KEY_NOTES));
                isiobat=c.getString(c.getColumnIndex(SQLiteDB.KEY_MEDICINE));
                isimoods=c.getString(c.getColumnIndex(SQLiteDB.KEY_MOODS));

                String item=id+"__"+isicatatan+"__"+isiobat+"__"+isimoods;
                records.add(item);

            }
        //notify listview of dataset changed
        adapter.notifyDataSetChanged();
    }

    //MEMASUKKAN SAMPLE DATA
    public void insertSampleData(){
        //get a writable database
        SQLiteDatabase database = dbhelper.getWritableDatabase();

        database.execSQL("INSERT INTO tabel_period1(notes,medicine,moods)"
                +" values ('Catatan 1','Obat1','Baik')");
        database.execSQL("INSERT INTO tabel_period1(notes,medicine,moods)"
                + " values ('Catatan 2','Obat2','kurang baik')");
        database.execSQL("INSERT INTO tabel_period1(notes,medicine,moods)"
                + " values ('Catatan 3','Obat3','tidak baik')");
        database.execSQL("INSERT INTO tabel_period1(notes,medicine,moods)"
                + " values ('Catatan 4','Obat4','sangat tidak baik')");
        database.execSQL("INSERT INTO tabel_period1(notes,medicine,moods)"
                + " values ('Catatan 5','Obat5','jelek')");
        database.execSQL("INSERT INTO tabel_period1(notes,medicine,moods)"
                + " values ('Catatan 5','Obat5','jelek sekali')");

    }

    // HAPUS DATA DARI TABEL
    public void deleteRow(View view){
        Button bt=(Button)view;
        final String del_id1=bt.getTag().toString();

    //MENAMPILKAN PERINGATAN MENGHAPUS DATA
        AlertDialog.Builder alertDialogHapus = new AlertDialog.Builder(MainActivity.this);
        alertDialogHapus.setTitle("Hapus Data");
        alertDialogHapus.setMessage("Anda yakin ingin menghapus data ini?");
    //KALAU YAKIN HAPUS
        alertDialogHapus.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                SQLiteDatabase database = dbhelper.getWritableDatabase();
                database.delete(SQLiteDB.MYDATABASE_TABLE, SQLiteDB.KEY_ID + "=?", new String[]{del_id1});
                for (int i = 0; i < records.size(); i++) {
                    if (records.get(i).startsWith(del_id1))
                        records.remove(i);
                }

                adapter.notifyDataSetChanged();
                countRecords_period();

                Toast.makeText(MainActivity.this, "Data berhasil dihapus", Toast.LENGTH_SHORT).show();
            }
        });

    //KALAU TIDAK YAKIN JANGAN DIHAPUS
        alertDialogHapus.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        alertDialogHapus.show();

    }

    // TAMPILKAN JUMLAH DATA YANG ADA DALAM TABEL
    public void countRecords_period() {
        SQLiteDatabase database=dbhelper.getWritableDatabase();

        String sql = "SELECT * FROM tabel_period1";
        int recordCount = database.rawQuery(sql, null).getCount();
        database.close();

        TextView textViewRecordCount = (TextView) findViewById(R.id.txt_data_pasien);
        textViewRecordCount.setText("DATA PASIEN (" + recordCount + " data)");
    }

    // EDIT DATA DARI TABEL
    public void editRow(View view){
        Button bt_edit = (Button)view;
        SQLiteDatabase database = dbhelper.getWritableDatabase();
        final String del_id = bt_edit.getTag().toString();
        String sql_ambil_1_data = "SELECT * FROM tabel_period1 WHERE _id = " + del_id;
        final Cursor cursor = database.rawQuery(sql_ambil_1_data, null);
        final Context context = view.getContext();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View formElementsView = inflater.inflate(R.layout.periode_editdata, null, false);

        final EditText enterid1 = (EditText) formElementsView.findViewById(R.id.masukkanid);
        final EditText enternotes1 = (EditText) formElementsView.findViewById(R.id.masukkannotes);
        final EditText entermedicine1 = (EditText) formElementsView.findViewById(R.id.masukkanmedicine);
        final EditText entermoods1 = (EditText) formElementsView.findViewById(R.id.masukkanmoods);

        if (cursor.moveToFirst()) {

            int id_per = Integer.parseInt(cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_ID)));
            String notes_per = cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_NOTES));
            String medicine_per = cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_MEDICINE));
            String mood_per = cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_MOODS));

        }

        enterid1.setText(del_id);
        enternotes1.setText(cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_NOTES)));
        entermedicine1.setText(cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_MEDICINE)));
        entermoods1.setText(cursor.getString(cursor.getColumnIndex(SQLiteDB.KEY_MOODS)));

        cursor.close();
        database.close();

        new AlertDialog.Builder(context)
                .setView(formElementsView)
                .setTitle("Edit Record")
                .setPositiveButton("Save Changes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                                String notesperiod2 = enternotes1.getText().toString();
                                String medicineperiod2 = entermedicine1.getText().toString();
                                String moodsperiod2 = entermoods1.getText().toString();
                                String idperiod2 = enterid1.getText().toString();

                                ContentValues values = new ContentValues();

                                values.put("notes", notesperiod2);
                                values.put("medicine", medicineperiod2);
                                values.put("moods", moodsperiod2);

                                String where = "_id = ?";

                                String[] whereArgs = { idperiod2 };

                                SQLiteDatabase database_period = dbhelper.getWritableDatabase();

                                boolean updateSuccessful = database_period.update("tabel_period1", values, where, whereArgs) > 0;

                                dialog.cancel();
                                adapter.clear();
                                readSampleData();
                            }

                        }).show();


        adapter.notifyDataSetChanged();
        countRecords_period();

    }

    // TAMBAH DATA KE TABEL
    public void tambahRow(View view){

        final Context context = view.getContext();

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View formElementsView = inflater.inflate(R.layout.periode_tambahdata, null, false);

        final EditText enternotes = (EditText) formElementsView.findViewById(R.id.masukkannotes);
        final EditText entermedicine = (EditText) formElementsView.findViewById(R.id.masukkanmedicine);
        final EditText entermoods = (EditText) formElementsView.findViewById(R.id.masukkanmoods);

        Button bt_tambah = (Button)view;
        SQLiteDatabase database = dbhelper.getWritableDatabase();

        new android.support.v7.app.AlertDialog.Builder(context)
                .setView(formElementsView)
                .setTitle("Tambah data period")
                .setPositiveButton("Simpan",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {

                                String notesperiod = enternotes.getText().toString();
                                String medicineperiod = entermedicine.getText().toString();
                                String moodsperiod = entermoods.getText().toString();


                                ContentValues values = new ContentValues();

                                values.put("notes", notesperiod);
                                values.put("medicine", medicineperiod);
                                values.put("moods", moodsperiod);


                                SQLiteDatabase database_period = dbhelper.getWritableDatabase();

                                boolean createSuccessful = database_period.insert("tabel_period1", null, values) > 0;
                                database_period.close();


                                if (createSuccessful) {
                                    Toast.makeText(context, "Data period baru BERHASIH DISIMPAN", Toast.LENGTH_SHORT).show();
                                   // listview.setSelection(adapter.getCount()-1);
                                } else {
                                    Toast.makeText(context, "Data period baru GAGAL BERHASIH DISIMPAN", Toast.LENGTH_SHORT).show();
                                }


                                dialog.cancel();
                                adapter.clear();
                                readSampleData();
                                countRecords_period();
                                //ATUR LISTVIEW KE AKHIR
                                listview.setSelection(adapter.getCount()-1);

                            }

                        }).show();

        adapter.notifyDataSetChanged();
        countRecords_period();
    }
}
Download source code disini
Semoga membantu...
%d blogger menyukai ini: