berikut xml dari layout, activity_main.xml
berikut xml dari layout row_wisata.xml
berikut DbHelper.java nya
package com.listviewsqlite.yonandroid; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DbHelper extends SQLiteOpenHelper{ private static final String DATABASE_NAME = "dbwisata"; public static final String NAMA = "nama"; public static final String KEY_ID = "_id"; public DbHelper(Context context){ super(context, DATABASE_NAME, null, 1); } //method createTable untuk Tabel Wisata public void createTable(SQLiteDatabase db){ db.execSQL("DROP TABLE IF EXISTS WISATA"); db.execSQL("CREATE TABLE if not exists WISATA (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + " nama TEXT);"); } //method generate data untuk mengisikan data ke table wisata public void generateData(SQLiteDatabase db){ ContentValues cv = new ContentValues(); cv.put(NAMA, "Pantai Siung"); db.insert("WISATA", NAMA, cv); cv.put(NAMA, "Gunung Merapi"); db.insert("WISATA", NAMA, cv); cv.put(NAMA, "Candi Borobudur"); db.insert("WISATA", NAMA, cv); cv.put(NAMA, "Benteng Vanderbug"); db.insert("WISATA", NAMA, cv); cv.put(NAMA, "Gunung Merbabu"); db.insert("WISATA", NAMA, cv); cv.put(NAMA, "Candi Prambanan"); db.insert("WISATA", NAMA, cv); cv.put(NAMA, "Bukit Bintang"); db.insert("WISATA", NAMA, cv); } //method delAlldata untuk menghapus data dari table public void delAllData(SQLiteDatabase db){ db.delete("WISATA", null, null); } public Cursor fetchAllWisata(SQLiteDatabase db){ return db.query("WISATA", new String[] { KEY_ID, NAMA }, null, null, null, null, null); } @Override public void onCreate(SQLiteDatabase db) { createTable(db); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } }
dan berikut MainActivity.java nya
package com.listviewsqlite.yonandroid; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class MainActivity extends Activity { private DbHelper dbhelper; private SQLiteDatabase db = null; private ListView listContent = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); dbhelper = new DbHelper(this); db = dbhelper.getWritableDatabase(); dbhelper.delAllData(db); dbhelper.generateData(db); setContentView(R.layout.activity_main); listContent = (ListView) findViewById(R.id.listWisata); isDataListView(); } @SuppressWarnings("deprecation") private void isDataListView() { Cursor wisataCursor; wisataCursor = dbhelper.fetchAllWisata(db); startManagingCursor(wisataCursor); String[] from = new String[]{dbhelper.NAMA}; int[] to = new int[]{R.id.tvWisata}; SimpleCursorAdapter wisataAdapter = new SimpleCursorAdapter(this, R.layout.row_wisata, wisataCursor, from, to); listContent.setAdapter(wisataAdapter); } @Override public void onDestroy() { super.onDestroy(); try{ db.close(); }catch (Exception e){ } } }
0 komentar:
Post a Comment