buat projek baru:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Nambah Data neng SQLite" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="NIM" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Jeneng" /> <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:layout_width="286dp" android:layout_height="wrap_content" android:text="Simpenn" /> </LinearLayout> |
MainActivity.java
package com.yusufruli.praktikum8;
import android.os.Bundle; import android.app.Activity; import android.database.sqlite.SQLiteDatabase; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private SQLiteDatabase db =null; private DBHelper PustakaDB =null; private EditText Nim, Nama; private Button Simpan; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PustakaDB = new DBHelper (this); db = PustakaDB.getWritableDatabase(); PustakaDB.createTableMahasiswa(db); Nim=(EditText) findViewById(R.id.editText1); Nama=(EditText) findViewById(R.id.editText2); Simpan=(Button) findViewById(R.id.button1); Simpan.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String isiNim= Nim.getText().toString(); String isiNama= Nama.getText().toString(); PustakaDB.addDataMahasiswa(db, isiNim, isiNama); Toast.makeText(MainActivity.this, "data telah disimpan", Toast.LENGTH_LONG).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } |
DBHelper.java
package com.yusufruli.praktikum8;
import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper{ private static final String DATABASE_NAME = "basisdata"; private static final int DATABASE_VERSION = 1; public DBHelper (Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub } public void createTableMahasiswa(SQLiteDatabase db){ db.execSQL("CREATE TABLE if not exists Mahasiswa (Nim TEXT PRIMARY KEY, Nama TEXT);"); } public void addDataMahasiswa(SQLiteDatabase databasis, String nim, String nama){ ContentValues NilaiBaru = new ContentValues(); NilaiBaru.put("Nim", nim); NilaiBaru.put("Nama", nama); databasis.insert("Mahasiswa", null, NilaiBaru); } public void editDataMahasiswa(SQLiteDatabase databasis, String nim, String nama){ ContentValues UbahNilai = new ContentValues(); UbahNilai.put("Nama", nama); databasis.update("nama", UbahNilai, "nim"+nim, null); } public void deleteDataMahasiswa(SQLiteDatabase databasis, String nim){ databasis.delete("nama", "nim"+nim, null); } } |
Hasil Running:
0 komentar:
Post a Comment