Ok bro langsung aje, disini ane membuat pengolahan data siswa dengan SQLite di android (ye iye lah di android, temanya aje Belajar Android...:) ), dimana berikut tampilan akhirnya setelah jadi :
Beikut file XML dari activity_main.xml :
>
Berikut file XML dari list_siswa.xml
Breikut file CLass dari MainActivity
package com.crudlistviewsqlite; import java.util.ArrayList; import java.util.HashMap; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; public class MainActivity extends ListActivity { Intent intent; TextView siswaId; DatabaseControl dbcontrol = new DatabaseControl(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //ambil data siswa dari Database.getAllsiswa ArrayList> siswaList = dbcontrol.getAllsiswa(); //jika data ada tampilkan data siswa di listview if (siswaList.size() != 0) { ListView lv = getListView(); lv.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView parent,View view, int position, long id){ siswaId = (TextView) view.findViewById(R.id.tvIdSiswa); String valsiswaId = siswaId.getText().toString(); Intent objintent = new Intent(getApplicationContext(), EditSiswaActivity.class); objintent.putExtra("siswaId", valsiswaId); startActivity(objintent); } }); ListAdapter adapter = new SimpleAdapter(MainActivity.this, siswaList, R.layout.list_siswa, new String[]{ "siswaId", "namaSiswa", "jurusan", "alamat"}, new int[] { R.id.tvIdSiswa, R.id.tvNamaSiswa, R.id.tvJurusan, R.id.tvAlamat}); setListAdapter(adapter); } } //tambah Siswa, jika mengklik tombl plus public void TambahSiswa(View view){ Intent objintent = new Intent(getApplicationContext(), SiswaBaruActivity.class); startActivity(objintent); } }
Berikut file Class dari DatabaseControl
package com.crudlistviewsqlite; import java.util.ArrayList; import java.util.HashMap; import android.util.Log; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseControl extends SQLiteOpenHelper{ private static final String LOGCAT = null; public DatabaseControl(Context applicationcontext) { super(applicationcontext, "siswa.db", null, 1); // TODO Auto-generated constructor stub Log.d(LOGCAT, "Created"); } // TODO Auto-generated method membuat tabel Siswa @Override public void onCreate(SQLiteDatabase database) { String query; query = "CREATE TABLE siswa ( siswaId INTEGER PRIMARY KEY, " + " namasiswa TEXT, jurusan TEXT, alamat TEXT)"; database.execSQL(query); Log.d(LOGCAT,"siswa Created"); } // TODO Auto-generated method Upgrade tabel siswa @Override public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) { String query; query = "DROP TABLE IF EXISTS siswa"; database.execSQL(query); onCreate(database); } //kode insert data ke dalam tabel siswa public void TambahSiswa(HashMapMasih ada dua file XML lagi, edit_datasiswa.xml dan tambah_data_siswa.xml. Dan dua file Class, SiswaBaruActivity dan EditSiswaActivity. Karna terlalu panjang dan hari sudah pagi, eh mw nonton Chelsea vs PSG dulu, silahkan Download lengkap FIle nya aja, Click disiniqueryValues){ SQLiteDatabase database = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("namaSiswa", queryValues.get("namaSiswa")); values.put("jurusan", queryValues.get("jurusan")); values.put("alamat", queryValues.get("alamat")); database.insert("siswa", null, values); database.close(); } //Tampilkan data Siswa ke listview, getallSiswa public ArrayList > getAllsiswa() { ArrayList > siswaList; siswaList = new ArrayList >(); String selectQuery = "SELECT * FROM siswa"; SQLiteDatabase database = this.getWritableDatabase(); Cursor cursor = database.rawQuery(selectQuery, null); if (cursor.moveToFirst()) { do { HashMap map = new HashMap (); map.put("siswaId", cursor.getString(0)); map.put("namaSiswa", cursor.getString(1)); map.put("jurusan", cursor.getString(2)); map.put("alamat", cursor.getString(3)); siswaList.add(map); }while (cursor.moveToNext()); } //return contact return siswaList; } public int editSiswa(HashMap queryValues){ SQLiteDatabase database = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("namaSiswa", queryValues.get("namaSiswa")); values.put("jurusan", queryValues.get("jurusan")); values.put("alamat", queryValues.get("alamat")); return database.update("siswa", values, "siswaId" + " = ?", new String[]{ queryValues.get("siswaId")}); } public void hapusSiswa(String id){ Log.d(LOGCAT, "delete"); SQLiteDatabase database = this.getWritableDatabase(); String deleteQuery = "DELETE FROM siswa where siswaId= '"+ id +"'"; Log.d("query", deleteQuery); database.execSQL(deleteQuery); } public HashMap getInfoSiswa(String id){ HashMap siswaList = new HashMap (); SQLiteDatabase database = this.getReadableDatabase(); String selectQuery = "SELECT * FROM siswa where siswaId='"+id+"'"; Cursor cursor = database.rawQuery(selectQuery, null); if(cursor.moveToFirst()){ do { siswaList.put("namaSiswa", cursor.getString(1)); siswaList.put("jurusan", cursor.getString(2)); siswaList.put("alamat", cursor.getString(3)); }while(cursor.moveToNext()); } return siswaList; } }
Happy Coding broo....:)
0 komentar:
Post a Comment