Professional Documents
Culture Documents
Aplikasi Biodata
Aplikasi Biodata
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEdtName = findViewById(R.id.edtName);
mEdtAge = findViewById(R.id.edtAge);
mEdtPhone = findViewById(R.id.edtPhone);
mBtnAdd = findViewById(R.id.btnAdd);
mBtnList = findViewById(R.id.btnList);
mImageView = findViewById(R.id.imageView);
//creating database
mSQLiteHelper = new SQLiteHelper(this, "RECORDDB.sqlite", null,
1);
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull
String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CODE_GALLERY){
if (grantResults.length>0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED){
//gallery intent
Intent galleryIntent = new
Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent,
REQUEST_CODE_GALLERY);
}
else {
Toast.makeText(this, "Don't have permission to access
file location", Toast.LENGTH_SHORT).show();
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
Model.java
public class Model {
private int id;
private String name;
private String age;
private String phone;
private byte[] image;
public Model(int id, String name, String age, String phone, byte[]
image) {
this.id = id;
this.name = name;
this.age = age;
this.phone = phone;
this.image = image;
}
RecordListActivity.java
public class RecordListActivity extends AppCompatActivity {
ListView mListView;
ArrayList<Model> mList;
RecordListAdapter mAdapter = null;
ImageView imageViewIcon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record_list);
mListView = findViewById(R.id.listView);
mList = new ArrayList<>();
mAdapter = new RecordListAdapter(this, R.layout.row, mList);
mListView.setAdapter(mAdapter);
mListView.setOnItemLongClickListener(new
AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView,
View view, final int position, long l) {
//alert dialog to display options of update and delete
final CharSequence[] items = {"Update", "Delete"};
dialog.setTitle("Choose an action");
dialog.setItems(items, new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface,
int i) {
if (i == 0){
//update
Cursor c =
MainActivity.mSQLiteHelper.getData("SELECT id FROM RECORD");
ArrayList<Integer> arrID = new
ArrayList<Integer>();
while (c.moveToNext()){
arrID.add(c.getInt(0));
}
//show update dialog
showDialogUpdate(RecordListActivity.this,
arrID.get(position));
}
if (i==1){
//delete
Cursor c =
MainActivity.mSQLiteHelper.getData("SELECT id FROM RECORD");
ArrayList<Integer> arrID = new
ArrayList<Integer>();
while (c.moveToNext()){
arrID.add(c.getInt(0));
}
showDialogDelete(arrID.get(position));
}
}
});
dialog.show();
return true;
}
});
imageViewIcon = dialog.findViewById(R.id.imageViewRecord);
final EditText edtName = dialog.findViewById(R.id.edtName);
final EditText edtAge = dialog.findViewById(R.id.edtAge);
final EditText edtPhone = dialog.findViewById(R.id.edtPhone);
Button btnUpdate = dialog.findViewById(R.id.btnUpdate);
mList.add(new Model(id,name,age,phone,image));
}
mAdapter.notifyDataSetChanged();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull
String[] permissions, @NonNull int[] grantResults) {
if (requestCode == 888){
if (grantResults.length>0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED){
//gallery intent
Intent galleryIntent = new
Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, 888);
}
else {
Toast.makeText(this, "Don't have permission to access
file location", Toast.LENGTH_SHORT).show();
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
@Override
public int getCount() {
return recordList.size();
}
@Override
public Object getItem(int i) {
return recordList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (row==null){
LayoutInflater inflater =
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
;
row = inflater.inflate(layout, null);
holder.txtName = row.findViewById(R.id.txtName);
holder.txtAge = row.findViewById(R.id.txtAge);
holder.txtPhone = row.findViewById(R.id.txtPhone);
holder.imageView = row.findViewById(R.id.imgIcon);
row.setTag(holder);
}
else {
holder = (ViewHolder)row.getTag();
}
holder.txtName.setText(model.getName());
holder.txtAge.setText(model.getAge());
holder.txtPhone.setText(model.getPhone());
byte[] recordImage = model.getImage();
Bitmap bitmap = BitmapFactory.decodeByteArray(recordImage, 0,
recordImage.length);
holder.imageView.setImageBitmap(bitmap);
return row;
}
}
SQLiteHelper.java
public class SQLiteHelper extends SQLiteOpenHelper{
//constructor
SQLiteHelper(Context context,
String name,
SQLiteDatabase.CursorFactory factory,
int version){
super(context, name, factory, version);
}
//insertData
public void insertData(String name, String age, String phone, byte[]
image){
SQLiteDatabase database = getWritableDatabase();
//query to insert record in database table
String sql = "INSERT INTO RECORD VALUES(NULL, ?, ?, ?, ?)";
//where "RECORD" is table name in database we will create in mainActivity
statement.bindString(1, name);
statement.bindString(2, age);
statement.bindString(3, phone);
statement.bindBlob(4, image);
statement.executeInsert();
}
//updateData
public void updateData(String name, String age, String phone, byte[]
image, int id){
SQLiteDatabase database = getWritableDatabase();
//query to update record
String sql = "UPDATE RECORD SET name=?, age=?, phone=?, image=?
WHERE id=?";
statement.bindString(1, name);
statement.bindString(2, age);
statement.bindString(3, phone);
statement.bindBlob(4, image);
statement.bindDouble(5, (double)id);
statement.execute();
database.close();
}
//deleteData
public void deleteData(int id){
SQLiteDatabase database = getWritableDatabase();
//query to delete record using id
String sql = "DELETE FROM RECORD WHERE id=?";
statement.execute();
database.close();
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ImageView
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:background="@color/colorPrimaryDark"
android:layout_width="160dp"
android:layout_height="160dp"
android:src="@drawable/addphoto"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"/>
<EditText
android:id="@+id/edtName"
android:layout_below="@id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name"
android:layout_centerHorizontal="true"
android:ems="10"/>
<EditText
android:id="@+id/edtAge"
android:layout_below="@id/edtName"
android:hint="Age"
android:layout_centerHorizontal="true"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edtPhone"
android:layout_below="@id/edtAge"
android:hint="Phone"
android:layout_centerHorizontal="true"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnAdd"
android:layout_below="@id/edtPhone"
android:layout_centerHorizontal="true"
android:minWidth="160dp"
android:text="Save Record"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnList"
android:layout_below="@id/btnAdd"
android:layout_centerHorizontal="true"
android:minWidth="160dp"
android:text="Record List"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</ScrollView>
activity_record_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RecordListActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
Row.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="3dp"
app:cardUseCompatPadding="true"
app:cardCornerRadius="3dp"
app:cardBackgroundColor="#fff"
app:contentPadding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical">
<ImageView
android:id="@+id/imgIcon"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:text="Name: "
android:textSize="20sp"
android:textColor="#000"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txtName"
android:text="Name Here"
android:textSize="20sp"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Age: "
android:textSize="20sp"
android:textColor="#000"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txtAge"
android:text="Age Here"
android:textSize="20sp"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow>
<TextView
android:text="Phone: "
android:textSize="20sp"
android:textColor="#000"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txtPhone"
android:text="Phone Here"
android:textSize="20sp"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Update_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="10dp">
<ImageView
android:id="@+id/imageViewRecord"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/colorPrimaryDark"
android:src="@drawable/addphoto"/>
<EditText
android:id="@+id/edtName"
android:hint="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"/>
<EditText
android:id="@+id/edtAge"
android:hint="Age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"/>
<EditText
android:id="@+id/edtPhone"
android:hint="Phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"/>
<Button
android:id="@+id/btnUpdate"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:text="Update"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>