PB2 Program 1
PB2 Program 1
XML Code:
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MEDICINE DATABASE"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="30dp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Insert Data"/>
<Switch
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Fetch Data"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/txtViewMed"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Medicine Name:" />
<EditText
android:id="@+id/edtTxtMed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
Mobile Application Development – 18CSMP68
2 Program 8: Medicine Database
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter Medicine Name Here" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/txtViewDate"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date:" />
<EditText
android:id="@+id/edtTxtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="date"
android:hint="Eg. DD/MM/YYYY" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/txtViewDayTime"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Time of the Day" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:entries="@array/timeOfDay"
/>
</LinearLayout>
<Button
android:id="@+id/btnInsert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
Mobile Application Development – 18CSMP68
3 Program 8: Medicine Database
<Button
android:id="@+id/btnFetch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Retrieve Data" />
</LinearLayout>
Java Code:
package com.example.medicinedb;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewMed = findViewById(R.id.txtViewMed);
medicineName = findViewById(R.id.edtTxtMed);
medicineDate = findViewById(R.id.edtTxtDate);
insertButton = findViewById(R.id.btnInsert);
fetchButton = findViewById(R.id.btnFetch);
fetchButton.setVisibility(View.INVISIBLE);
dayTimeSpinner = findViewById(R.id.spinner);
swtch = findViewById(R.id.switcher);
//STEP 2
//STEP 3:
swtch.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
//Checked whether the switch is checked to enable some
components
if(!isChecked){
fetchButton.setVisibility(View.INVISIBLE);
insertButton.setVisibility(View.VISIBLE);
medicineName.setVisibility(View.VISIBLE);
textViewMed.setVisibility(View.VISIBLE);
}
else{
//Enable some of the buttons and components (Views) as
Visible
medicineName.setVisibility(View.INVISIBLE);
insertButton.setVisibility(View.INVISIBLE);
textViewMed.setVisibility(View.INVISIBLE);
fetchButton.setVisibility(View.VISIBLE);
//STEP 10:
fetchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String date = medicineDate.getText().toString();
String time = dayTimeSpinner.getSelectedItem().toString();
String med = "";
Cursor cu = dbConnection.RetrieveData(date, time);
Mobile Application Development – 18CSMP68
5 Program 8: Medicine Database
//Since the cursor will return more than one row, we need
to traverse through the records/rows
cu.moveToFirst();
do{
//med=med+(String.valueOf(cursor.getString(cursor.getColumnIndex("medicineN
ame"))));
med =
med+(String.valueOf(cu.getString(cu.getColumnIndex("medicineName"))));
med+="\n";
}while (cu.moveToNext());
//Once done, PRINT a confirmatory Toast Message
Toast.makeText(getApplicationContext(), med,
Toast.LENGTH_LONG).show();
}
});
}
Database Connection:
package com.example.medicinedb;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
@Override
public void onCreate(SQLiteDatabase dbase) {
dbase.execSQL("create Table MedTable(medicineName TEXT primary key,
date TEXT, time TEXT)");//Creates the table
}
@Override
public void onUpgrade(SQLiteDatabase dbase, int i, int i1) { //NOTE:
i=oldVersion, i1=newVersion
}
//STEP 8: Write what should happen next
public boolean insertValues(String medName, String medDate, String
medTime){
SQLiteDatabase database = this.getWritableDatabase();//Sort of
Opens the database for read/write
ContentValues contentValues = new ContentValues();//Stores the set
of values ie,name, date & time
contentValues.put("medicineName",medName); //Uses KEY-Value Pair to
store those values
contentValues.put("date",medDate);
contentValues.put("time",medTime);
long result = database.insert("MedTable",null,contentValues);
if(result==-1)//meaning that the values where not inserted..
return false;
else
return true;
}
public Cursor RetrieveData(String date, String time){
//Open the Database in Readable Mode
SQLiteDatabase database = this.getReadableDatabase();
Cursor cursor = database.rawQuery("Select * from MedTable where
date= '"+date+"' AND time='"+time+"'", null);
return cursor;
//Go BACK and Update MainActivity.java to update the database
//Make Sure you refer the fetchButton
}
}
Output: