0% found this document useful (0 votes)
18 views4 pages

Koneksi

The document describes 4 Java classes used in an Android application for managing customer data in a SQLite database. The MainActivity class controls the main screen and allows launching a customer form or browsing data. The DBAdapter class defines methods for the database operations. The CustomerForm class manages the customer entry form. The SaveCustomer class inserts a new customer record into the database.

Uploaded by

agungnurhakim410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Koneksi

The document describes 4 Java classes used in an Android application for managing customer data in a SQLite database. The MainActivity class controls the main screen and allows launching a customer form or browsing data. The DBAdapter class defines methods for the database operations. The CustomerForm class manages the customer entry form. The SaveCustomer class inserts a new customer record into the database.

Uploaded by

agungnurhakim410
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1. MainActivity.

java

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnNew = (Button) findViewById(R.id.NewCust);
//btnNew.setOnClickListener(this);
btnNew.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, CustomerForm.class);
startActivity(i);
}
} );

Button btnBrowse = (Button) findViewById(R.id.BrowseCust);


/btnBrowse.setOnClickListener(this);
btnBrowse.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DBAdapter db = new DBAdapter(MainActivity.this);
db.open();
Cursor c = db.getAllCustomers();
if (c.moveToFirst()) {
do {
Toast.makeText(MainActivity.this, c.getString(1) + ", " +
c.getString(2), Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
else
Toast.makeText(MainActivity.this, "No data", Toast.LENGTH_SHORT).show();
db.close();
}
} );
}

2. DBAdapter.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {


private static final String TAG="DBAdapter";
private static final String DATABASE_NAME="mycompany.sqlite";
private static final int DATABASE_VERSION=1;
private static final String TABLE_CREATE = "create table customers (
_id integer primary key autoincrement, "
+ "custname text not null, custaddr text not null, "
+ "custgender text not null, custphone text not null)";
private static final String TABLE_DROP = "DROP TABLE IF EXISTS customers";

public static final String KEY_ROWID="_id";


public static final String KEY_CUSTNAME="custname";
public static final String KEY_CUSTADDR="custaddr";
public static final String KEY_CUSTGENDER="custgender";
public static final String KEY_CUSTPHONE="custphone";
private final Context context;
private DatabaseHelper dbHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx) {


this.context = ctx;
dbHelper = new DatabaseHelper(this.context);
}

private static class DatabaseHelper extends SQLiteOpenHelper {


DatabaseHelper(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " +
newVersion + ", which will destroy all old data");
db.execSQL(TABLE_DROP);
onCreate(db);
}
}

public DBAdapter open() throws SQLException {


db = dbHelper.getWritableDatabase();
return this;
}

public void close() {


dbHelper.close();
}

public long insertCustomer(String custName, String custAddr, char


custGender, String custPhone) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_CUSTNAME, custName);
initialValues.put(KEY_CUSTADDR, custAddr);
initialValues.put(KEY_CUSTGENDER, Character.toString(custGender));
initialValues.put(KEY_CUSTPHONE, custPhone);

return db.insert("customers", null, initialValues);


}

public Cursor getAllCustomers() {


return db.query("customers", new String[] {
KEY_ROWID, KEY_CUSTNAME, KEY_CUSTADDR, KEY_CUSTGENDER,
KEY_CUSTPHONE
}, null, null, null, null, KEY_ROWID + " DESC");
}
}
3. CustomerForm.java
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

public class CustomerForm extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer_form);

Button btnEducation = (Button) findViewById(R.id.Save);


btnEducation.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String custName = ((EditText)
findViewById(R.id.CustName)).getText().toString().trim();
String custAddr = ((EditText)
findViewById(R.id.CustAddr)).getText().toString().trim();
String custPhone = ((EditText)
findViewById(R.id.CustPhone)).getText().toString().trim();
char custGender = 'X';

switch (((RadioGroup)
findViewById(R.id.CustGender)).getCheckedRadioButtonId()) {
case R.id.GMale:
custGender='M';
break;
case R.id.GFemale:
custGender='F';
break;
}
Context context=CustomerForm.this;

if(custName.equals("") || custAddr.equals("") || custPhone.equals("")


|| custGender == 'X') {
String e = "Please complete the data.";
new AlertDialog.Builder(context)
.setTitle("Invalid Data")
.setMessage(e)
.setNeutralButton("Close", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which){
}
}).show();}
else {
//If OK, then send the data to save
Intent i = new Intent(CustomerForm.this, SaveCustomer.class);
i.putExtra("CustName", custName);
i.putExtra("CustAddr", custAddr);
i.putExtra("CustPhone", custPhone);
i.putExtra("CustGender", custGender);
startActivity(i);
}}
} );
}
}
4. SaveCustomer.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class SaveCustomer extends Activity{


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.save_customer);

//get the inputed data


Intent i = getIntent();
Bundle b = i.getExtras();

String custName = b.getString("CustName");


String custAddr = b.getString("CustAddr");
String custPhone = b.getString("CustPhone");
char custGender = b.getChar("CustGender");

DBAdapter db = new DBAdapter(this);

try {
db.open();
long id = db.insertCustomer(custName,custAddr,custGender,custPhone);
Toast
.makeText(this, "Data successfully saved", Toast.LENGTH_SHORT)
.show();
}
catch(Exception ex) {
Toast
.makeText(this, "Saving error", Toast.LENGTH_SHORT)
.show();
}
finally {
db.close();
}

Button btnBack = (Button) findViewById(R.id.Button01);


btnBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(SaveCustomer.this, MainActivity.class);
startActivity(i);
}
} );
}
}

You might also like