0% found this document useful (0 votes)
18 views

PB2 Program 1

The document describes an Android application for a medicine database. It includes XML layout code and Java code to create screens for inserting and fetching medicine data from a SQLite database. The code allows toggling between an insert and fetch interface and includes methods for inserting and retrieving medicine records from the database.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

PB2 Program 1

The document describes an Android application for a medicine database. It includes XML layout code and Java code to create screens for inserting and fetching medicine data from a SQLite database. The code allows toggling between an insert and fetch interface and includes methods for inserting and retrieving medicine records from the database.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1 Program 8: Medicine Database

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

android:text="Insert Data" />

<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;

public class MainActivity extends AppCompatActivity {


EditText medicineName,medicineDate;
TextView textViewMed;
Button insertButton, fetchButton;
Spinner dayTimeSpinner;
Switch swtch;
DbConnection dbConnection;
//STEP 1

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

dbConnection = new DbConnection(this);

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

//VIP: Provide functionality for switch to toggle blw InsertData /


Fetch Data

//STEP 3:
swtch.setOnCheckedChangeListener(new

Mobile Application Development – 18CSMP68


4 Program 8: Medicine Database

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);

//End of STEP 3....NOW RUN AND SHOW THEM


}
}
});

//STEP 4: Create the functionality for INSSERT BUTTON


insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = medicineName.getText().toString();
String date = medicineDate.getText().toString();
String time = dayTimeSpinner.getSelectedItem().toString();

//STEP 5: We now have to create a Database and write SQL


Queries to
//..INSERT items into the database as...
//Create a Class Called DbConnection and overide the
methods accordingly

//After STEP 8, do this STEP 9


//First create an object of DbConection and initialize
above (in onCreate Method)
boolean insert = dbConnection.insertValues(name,date,time);
if(insert==true){
Toast.makeText(getApplicationContext(),"Data inserted
Successfully", Toast.LENGTH_SHORT).show();
medicineName.setText(null);
medicineDate.setText(null);//This allows us to insert
new data
}
else //if otherwise
Toast.makeText(getApplicationContext(), "Data insertion
unsuccessful", Toast.LENGTH_SHORT).show();
}
});

//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;

public class DbConnection extends SQLiteOpenHelper {

public DbConnection(Context context) {


super(context, "MedicineDBase", null, 1);
}

//STEP 7: Now, override two functions viz: onCreate and onUpgrade

@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

Mobile Application Development – 18CSMP68


6 Program 8: Medicine Database

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
}
}

Mobile Application Development – 18CSMP68


7 Program 8: Medicine Database

Output:

Mobile Application Development – 18CSMP68

You might also like