Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Pengelolaan Database Lanjutan 2

Presentasi serupa


Presentasi berjudul: "Pengelolaan Database Lanjutan 2"— Transcript presentasi:

1 Pengelolaan Database Lanjutan 2
Pemrograman Bergerak Pengelolaan Database Lanjutan 2 1

2 Update public int update (String table, ContentValues values, String whereClause, String[] whereArgs) Table, nama tabel Values, data map yang berisi pasangan kolom dan nilai yang akan diupdate WhereClause, Where clause pada SQL, null jika semua record WhereArgs, list yang berisi nilai untuk masing-masing parameter ? Pada Where Clause Returns Jumlah baris yang terpengaruh.

3 Contoh ContentValues initialValues = new ContentValues();
initialValues.put("nim", "9224"); initialValues.put("nilai", "B"); Integer hasil = db.update("Siswa", initialValues, "nim=?", new String[] {" "});

4 Delete Table, nama tabel
public int delete (String table, String whereClause, String[] whereArgs) Table, nama tabel WhereClause, Where clause pada SQL, null jika semua record WhereArgs, list yang berisi nilai untuk masing-masing parameter ? Pada Where Clause Returns Jumlah baris yang terpengaruh.

5 Contoh atau menghapus seluruh baris
Integer hasil = db.delete("Siswa","nim=?", new String[] {"9224" }); atau menghapus seluruh baris Integer hasil = db.delete("Siswa",null, null);

6 Contoh Aplikasi

7 public void gotoEdit(String nim) {
Cursor cursor = db.query(false, "Siswa", new String[] { "nim", "nama", "nilai" }, "nim=?", new String[] { nim }, null, null, null, null); if (cursor.moveToNext()) { Intent intent = new Intent(); intent.setClass(this, PerbaikiData.class); intent.putExtra("nim", cursor.getString(0)); intent.putExtra("nama", cursor.getString(1)); intent.putExtra("nilai", cursor.getString(2)); startActivityForResult(intent, 2); } public void gotoHapus(String nim) { long hasil = db.delete("Siswa", "nim=?",new String[] {nim}); if (hasil < 1) { tampilToast("Data Gagal Disimpan!"); } else { refreshView(); private void tampilToast(String pesan) { int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(getApplicationContext(), pesan, duration); toast.show();

8 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); openDatabase(); refreshView(); ListView listView = (ListView) findViewById(android.R.id.list); //edit data listView.setOnItemClickListener(new ListView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { TextView baris1 = (TextView) v.findViewById(R.id.baris1); String selectedNim = baris1.getText().toString(); gotoEdit(selectedNim); } }); //hapus data listView.setOnItemLongClickListener(new ListView.OnItemLongClickListener() { public boolean onItemLongClick(AdapterView<?> parent, View v, final String selectedNim = baris1.getText().toString(); TextView baris2 = (TextView) v.findViewById(R.id.baris2); String selectedNama = baris2.getText().toString(); AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext()); builder.setMessage("Apakah anda yakin hapus data Nim " + selectedNim + " dengan Nama " + selectedNama +" ?") .setCancelable(false) .setPositiveButton("Ya", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { gotoHapus(selectedNim); }) .setNegativeButton("Tidak", null); AlertDialog alert = builder.create(); alert.show(); return false;

9 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK && requestCode == 1) { ContentValues initialValues = new ContentValues(); initialValues.put("nim", data.getStringExtra("nim")); initialValues.put("nama", data.getStringExtra("nama")); initialValues.put("nilai", data.getStringExtra("nilai")); long hasil = db.insert("Siswa", null, initialValues); if (hasil < 0) { tampilToast("Data Gagal Disimpan!"); } else { refreshView(); } } else if (resultCode == RESULT_OK && requestCode == 2) { String nim = data.getStringExtra("nim"); initialValues.put("nim", nim); long hasil = db.update("Siswa", initialValues, "nim=?",new String[] {nim}); if (hasil < 1) {

10 package com.tkj; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class PerbaikiData extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.perbaikidata); Intent intent = getIntent(); Bundle bundle = intent.getExtras(); if (bundle != null) { TextView nim = (TextView) findViewById(R.id.textNim); TextView nama = (TextView) findViewById(R.id.editNama); TextView nilai = (TextView) findViewById(R.id.editNilai); nim.setText(bundle.getString("nim")); nama.setText(bundle.getString("nama")); nilai.setText(bundle.getString("nilai")); } public void buttonSimpanOnClick(View v) { Intent intent = new Intent(); intent.putExtra("nim", nim.getText().toString()); intent.putExtra("nama", nama.getText().toString()); intent.putExtra("nilai", nilai.getText().toString()); this.setResult(RESULT_OK, intent); finish();

11 Sekian & Terima Kasih 11


Download ppt "Pengelolaan Database Lanjutan 2"

Presentasi serupa


Iklan oleh Google