Contoh Ujian Akhir Semester - Mobile1


Tips!

Contoh berikut menggunakan tabel dengan nama tablename dengan 2 field yaitu _id dan nama.
Anda tinggal menyesuaikan dengan soal dan tipe data yang digunakan.

DBHelper.java - onCreate

  public void onCreate(SQLiteDatabase db) {
      db.execSQL("CREATE TABLE tablename (_id INTEGER PRIMARY KEY AUTOINCREMENT, nama TEXT)");      
  }

DBHelper.java - addRecord

  public void addRecord(String valnama) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put("nama", valnama);
    db.insert("tablename", null, cv);
  }

DBHelper.java - deleteRecord

  public void deleteRecord(int _idRec) {
    SQLiteDatabase db = this.getWritableDatabase();  
    db.delete("tablename", "_id = " + _idRec, null);
  }

DBHelper.java - getRecord

  public ArrayList<String> getRecord() {
    SQLiteDatabase db = this.getWritableDatabase();
    ArrayList<String> data = new ArrayList<String>();
    Cursor c = db.rawQuery("SELECT * FROM tablename", null);
    while(c.moveToNext()) {
      data.add(String.format("%d, %s", c.getInt(0), c.getString(1)));
    }
    return data;
  }

RadioGroup

  int idcek = radiogrp.getCheckedRadioButtonId();
  RadioButton radiobtn = (RadioButton) findViewById(idcek);

  int tarif = 0;
  if (radiobtn.getText().equals("Umum")) tarif = 3000;
  else if (radiobtn.getText().equals("Member")) tarif = 2000;
  else tarif = 2500;

Button - Save addRecord

  public class MainActivity extends ListActivity implements OnClickListener{
    Button btn1;
    DBHelper db = new DBHelper(this, "uas5sia1", null, 1);

    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      Button btn1 = (Button) findViewById(R.id.button1);

      btn1.setOnClickListener(this);
    }

    public void onClick(View v) {
      db.addRecord("Mahasiswa");
      // Tampilkan data ke ListView
    }
  }

AlertDialog

alt

  AlertDialog.Builder msg = new AlertDialog.Builder(this);
  msg.setTitle("Apakah Kamu Ingin Keluar?");
  msg.setMessage("Ingin Keluar Dari Aplikasi Ini?");
  msg.setNegativeButton("TIDAK",null);
  msg.setPositiveButton("YA", new DialogInterface.OnClickListener() {                        
    public void onClick(DialogInterface dialog, int which) {

    }
  });
  msg.show();