Beranda > Android, Android Studio, Pemrograman > Android – Menampilkan Data SQL Ke Dalam Listview

Android – Menampilkan Data SQL Ke Dalam Listview

28 Maret 2016

logo android studio

Dalam pemrograman database sering kita ingin data dari SQL database ke sebuah listview dengan tujuan untuk memperlihatkan data dengan banyak field, seperti gambar dibawah ini.

data pasien

File yang dibutuhkan adalah :

1. rowlayout.xml
2. activity_main.xml
3. DbAdapter.java
4. MainActivity.java

Berikut langkah-langkah menampilkan data SQL ke dalam listview:

1 Buka android studio dan buat sebuah project baru.

2 Buat file rowlayout.xml 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="110dp">
 
<TextView 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tempat Catatan"
android:id="@+id/tempatcatatan"
android:layout_below="@+id/tempatid"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#fffafbff" />
 
<TextView 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tempat Medicine"
android:id="@+id/tempatmedicine"
android:layout_below="@+id/tempatcatatan"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#fffafbff" />
 
<TextView 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tempat ID"
android:id="@+id/tempatid"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ffe7f0f0" />
 
<TextView 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tempat Medicine"
android:id="@+id/tempatmoods"
android:phoneNumber="true"
android:layout_below="@+id/tempatmedicine"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#fffafbff" />
 
</RelativeLayout>

3 Edit file activity_main.xml menjadi seperti ini.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#ff00a6f0"
android:layout_height="match_parent">
 
<TextView 
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Data Obat Pasien :"
android:id="@+id/textView"
android:textColor="#ffffffff"
android:layout_marginLeft="100dp" />
 
<ListView 
android:layout_width="350dp"
android:layout_height="wrap_content"
android:id="@+id/contentlist"
android:layout_below="@+id/textView"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp" />
</RelativeLayout>

4 Buat File DbAdapter.java

package com.wordpress.yopiardinal.listview4;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DbAdapter {

    public static final String MYDATABASE_NAME = "womancalendar1";
    public static final String MYDATABASE_TABLE = "tabel_period1";
    public static final int MYDATABASE_VERSION = 1;
    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_DATABASE =
            "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 MEMASUKKAN DATA DALAM TABEL-------------//
    private static final String SCRIPT_MASUK_DATA1=  "INSERT INTO tabel_period1(notes,medicine,moods)"
            +" values ('Catatan 1','Obat1','Baik')";
    private static final String SCRIPT_MASUK_DATA2=  "INSERT INTO tabel_period1(notes,medicine,moods)"
            +" values ('Catatan 2','Obat2','Ga ok')";
    private static final String SCRIPT_MASUK_DATA3=  "INSERT INTO tabel_period1(notes,medicine,moods)"
            +" values ('Catatan 3','Obat3','Bagus')";
    private static final String SCRIPT_MASUK_DATA4=  "INSERT INTO tabel_period1(notes,medicine,moods)"
            +" values ('Catatan 4','Obat4','jelex')";
    private static final String SCRIPT_MASUK_DATA5=  "INSERT INTO tabel_period1(notes,medicine,moods)"
            +" values ('Catatan 5','Obat5','Sakit')";
    private static final String SCRIPT_MASUK_DATA6=  "INSERT INTO tabel_period1(notes,medicine,moods)"
            +" values ('Catatan 6','Obat6','Stress')";
    private static final String SCRIPT_MASUK_DATA7=  "INSERT INTO tabel_period1(notes,medicine,moods)"
            +" values ('Catatan 7','Obat7','Stress Banget')";
    private static final String SCRIPT_MASUK_DATA8=  "INSERT INTO tabel_period1(notes,medicine,moods)"
            +" values ('Catatan 8','Obat8','Hampir Gila')";
    private static final String SCRIPT_MASUK_DATA9=  "INSERT INTO tabel_period1(notes,medicine,moods)"
            +" values ('Catatan 9','Obat9','Gila')";


    private SQLiteHelper sqLiteHelper;
    private SQLiteDatabase sqLiteDatabase;
    private Context context;

    public DbAdapter(Context c){
        context = c;
    }

    public DbAdapter openToRead() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getReadableDatabase();
        return this;
    }

    public DbAdapter openToWrite() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        return this;
    }

    public void close(){
        sqLiteHelper.close();
    }

    public long insert(String content1, String content2, String content3){

        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_NOTES, content1);
        contentValues.put(KEY_MEDICINE, content2);
        contentValues.put(KEY_MOODS, content2);
        return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
    }

    public int deleteAll(){
        return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
    }

    public Cursor queueAll(){
        String[] columns = new String[]{KEY_ID, KEY_NOTES, KEY_MEDICINE,KEY_MOODS};
        Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
                null, null, null, null, null);

        return cursor;
    }

    public class SQLiteHelper extends SQLiteOpenHelper {

        public SQLiteHelper(Context context, String name,
                            CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            //-------------MEMBUAT TABEL DAN MEMASUKKAN DATA DALAM TABEL-------------//
            // TODO Auto-generated method stub
            db.execSQL(SCRIPT_CREATE_DATABASE);
            db.execSQL(SCRIPT_MASUK_DATA1);
            db.execSQL(SCRIPT_MASUK_DATA2);
            db.execSQL(SCRIPT_MASUK_DATA3);
            db.execSQL(SCRIPT_MASUK_DATA4);
            db.execSQL(SCRIPT_MASUK_DATA5);
            db.execSQL(SCRIPT_MASUK_DATA6);
            db.execSQL(SCRIPT_MASUK_DATA7);
            db.execSQL(SCRIPT_MASUK_DATA8);
            db.execSQL(SCRIPT_MASUK_DATA9);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
        }
    }
}

5 Terakhir edit file MainActivity.java menjadi seperti ini.

package com.wordpress.yopiardinal.listview4;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView texttempatcatatan, texttempatmedicine,texttempatid,texttempatmoods;
    Button buttonAdd, buttonDeleteAll;

    private DbAdapter SQLAdapter;
    ListView listContent;

    SimpleCursorAdapter cursorAdapter;
    Cursor cursor;


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



        texttempatid = (TextView)findViewById(R.id.tempatid);
        texttempatcatatan = (TextView)findViewById(R.id.tempatcatatan);
        texttempatmedicine = (TextView)findViewById(R.id.tempatmedicine);
        texttempatmoods = (TextView)findViewById(R.id.tempatmoods);


        listContent = (ListView)findViewById(R.id.contentlist);

        SQLAdapter = new DbAdapter(this);
        SQLAdapter.openToWrite();

        cursor = SQLAdapter.queueAll();
        String[] from = new String[]{DbAdapter.KEY_ID, DbAdapter.KEY_NOTES, DbAdapter.KEY_MEDICINE, DbAdapter.KEY_MOODS};
        int[] to = new int[]{R.id.tempatid, R.id.tempatcatatan, R.id.tempatmedicine,R.id.tempatmoods};
        cursorAdapter =new SimpleCursorAdapter(this, R.layout.rowlayout, cursor, from, to);
        listContent.setAdapter(cursorAdapter);

    }

}

Downlod source code disini.

Diambil dari berbagai sumber.

Semoga membantu…

%d blogger menyukai ini: