Visvesvaraya Technological University: "Book Library"

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

VISVESVARAYA TECHNOLOGICAL

UNIVERSITY “Jnana Sangama”, Belagavi – 590 018, Karnataka

MINI PROJECT REPORT

ON

“Book Library”
Submitted in partial fulfillment of the requirements for the
Mobile Application Development Lab (18CSMP68)

BACHELOR OF ENGINEERING

IN

COMPUTER SCIENCE & ENGINEERING


Submitted by:
VISHAL.S.NAIK : 1JT19CS104

under the guidance of

NAGARAJ. A
Associate Professor
Department of Computer Science and Engineering

Department of Computer Science and Engineering


Jyothy Institute of Technology
Tataguni, Off. Kanakapura Road,
Bengaluru – 560 082
2021 – 2022
JYOTHY INSTITUTE OF TECHNOLOGY
Department of Computer Science and Engineering
Accredited by NBA, New Delhi
Tataguni, Off. Kanakapura Road,
Bengaluru - 560 082

CERTIFICATE

This is to certify that the Mini Project titled 'Book Library' carried out by Mr/Ms
VISHAL.S.NAIK, USN: 1JT19CS104, a bonafide student of Jyothy Institute of
Technology, in partial fulfillment for the award of Bachelor of Engineering, in
Computer Science and Engineering under Visvesvaraya Technological University, Belagavi,
during the year 2021- 2022.

The project report has been approved, as it satisfies the academic requirements in respect of
Mini Project prescribed for the course Mobile Application Development Lab (18CSMP68).

Signature of Faculty Incharge Signature of HOD

........................................... .…..............................
Prof. Nagaraj. A Dr. Prabhanjan. S.
Associate Professor Prof & Head of Dept - CSE
Dept of CSE JIT, Bengaluru
JIT, Bengaluru

External Viva

Name of the Examiners Signature with Date

1)

2)
DECLARATION

I, Vishal.S.Naik, 6th Semester student of Computer Science


and Engineering, Jyothy Institute of Technology, Bangalore -
560 082, declare that the Mini Project of Mobile Application
Development Lab (18CSMP68) is successfully completed.
This report is submitted in partial fulfillment of the
requirements for award of Bachelor Degree in Computer
Science and Engineering, during the academic year 2021-
2022.

Date : 13-07-2022
Place : Bangalore

USN : 1JT19CS104
Name : Vishal.S.Naik
ACKNOWLEDGEMENT

This Mini Project is a result of accumulated guidance, direction and support of


several important persons. I take this opportunity to express my gratitude to all,
who have helped us to complete the Mini Project.

I express our sincere thanks to our Principal Dr K. Gopalakrishna, for providing


us adequate facilities to undertake this Mini Project.

I would like to thank Dr. Prabhanjan. S, Head of Dept - CSE, for providing us an
opportunity to carry out Mini Project and for his valuable guidance and support.

I would like to thank, Prof. Nagaraj. A, Faculty In Charge, for guiding us during
the period of Mini Project.

I would like to thank all the faculty members of CSE department for the support
extended during the course of Mini Project.

I would like to thank the non-teaching members of the Dept of CSE, for helping
us during the Mini Project.

Last but not the least, I would like to thank our parents and friends without
whose constant help, the completion of Mini Project would have not been
possible.

VISHAL.S.NAIK [ 1JT19CS104 ]
Table of Contents
Sr No Description Page No
01 Problem statement with input and
output diagram
02 XML files code

03 Java files code

04 Screen shots
CHAPTER 1

Problem statement
with
input and output diagram
BOOK LIBRARY

Problem Statement:
Develop an android application - Library management application that helps the user to manage by
adding a Book, delete a book, search a book, update and display all the current available Books in form
of a list.

Input and Output Diagram:

Dept of CSE, JIT 2021-2022 7


CHAPTER 2

XML files Code


BOOK LIBRARY

activity_add.xml:

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
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=".AddActivity"
android:padding="30dp">
<EditText
android:id="@+id/title_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:ems="10"
android:hint="Book Title"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/author_input"
android:layout_width="match_parent"x
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Book Author"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_input" />
<EditText
android:id="@+id/pages_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Number of pages"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/author_input" />
<Button
android:id="@+id/add_button"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="40dp"
android:text="Add"
android:textSize="20sp"
android:textAllCaps="false"

Dept of CSE, JIT 2021-2022 9


BOOK LIBRARY

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pages_input" />
<Button
android:id="@+id/btnView"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="160dp"
android:text="View all data"
android:textAllCaps="false"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pages_input" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_update.xml:

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
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=".AddActivity"
android:padding="30dp">
<EditText
android:id="@+id/title_input2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:ems="10"
android:hint="Book Title"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/author_input2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Book Author"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_input2" />
<EditText
android:id="@+id/pages_input2"
android:layout_width="match_parent"
android:layout_height="wrap_content"

Dept of CSE, JIT 2021-2022 10


BOOK LIBRARY

android:layout_marginTop="16dp"
android:ems="10"
android:hint="Number of pages"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/author_input2" />
<Button
android:id="@+id/update_button"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="40dp"
android:text="Update"
android:textSize="20sp"
android:textAllCaps="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pages_input2" />
<Button
android:id="@+id/delete_button"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginTop="16dp"
android:text="Delete"
android:textAllCaps="false"
android:textColor="@color/red"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/update_button" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
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">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="28dp"
android:layout_marginRight="28dp"
android:layout_marginBottom="28dp"
android:clickable="true"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"

Dept of CSE, JIT 2021-2022 11


BOOK LIBRARY

app:srcCompat="@drawable/ic_add" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:paddingTop="?attr/actionBarSize"
android:layout_width="409dp"
android:layout_height="729dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/empty_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="230dp"
android:alpha="0.1"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_empty" />
<TextView
android:id="@+id/no_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="No Data."
android:textSize="20sp"
android:alpha="0.4"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/empty_imageview" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_home.xml:

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
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=".HomeActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="58dp"
android:layout_marginLeft="58dp"
android:layout_marginTop="300dp"
android:layout_marginEnd="59dp"

Dept of CSE, JIT 2021-2022 12


BOOK LIBRARY

android:layout_marginRight="59dp"
android:text="BOOKHOUSE"
android:textColor="@color/cardview_dark_background"
android:textSize="50dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnmain"
android:layout_width="377dp"
android:layout_height="62dp"
android:layout_marginTop="400dp"
android:text="Add Books"
android:textAllCaps="false"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

my_row.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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/mainLayout">
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">
<TextView
android:id="@+id/book_id_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="40sp"
android:textStyle="bold"
android:textColor="#000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/book_title_txt"

Dept of CSE, JIT 2021-2022 13


BOOK LIBRARY

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:text="Title 1"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000"
app:layout_constraintStart_toEndOf="@+id/book_id_txt"
app:layout_constraintTop_toTopOf="@+id/book_id_txt" />
<TextView
android:id="@+id/book_author_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Author1"
app:layout_constraintStart_toStartOf="@+id/book_title_txt"
app:layout_constraintTop_toBottomOf="@+id/book_title_txt"/>
<TextView
android:id="@+id/book_pages_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="120"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>

Dept of CSE, JIT 2021-2022 14


CHAPTER 3

Java files Code


BOOK LIBRARY

HomeActivity.java:

package com.example.Library.MadProject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class HomeActivity extends AppCompatActivity implements


View.OnClickListener {
Button btnBook;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
btnBook = (Button) findViewById(R.id.btnmain);
btnBook.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this, AddActivity.class);
startActivity(intent);
}
}

MainActivity.java:

package com.example.Library.MadProject;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.material.floatingactionbutton. FloatingActionButton;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
FloatingActionButton add_button;
ImageView empty_imageview;

Dept of CSE, JIT 2021-2022 16


BOOK LIBRARY

TextView no_data;

MyDatabaseHelper myDB;
ArrayList<String> book_id, book_title, book_author, book_pages;
CustomAdapter customAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
add_button = findViewById(R.id.add_button);
empty_imageview = findViewById(R.id.empty_imageview);
no_data = findViewById(R.id.no_data);
add_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,
AddActivity.class);
startActivity(intent);
}
});
myDB = new MyDatabaseHelper(MainActivity.this);
book_id = new ArrayList<>();
book_title = new ArrayList<>();
book_author = new ArrayList<>();
book_pages = new ArrayList<>();
storeDataInArrays();
customAdapter = new CustomAdapter(MainActivity.this,this, book_id,
book_title, book_author, book_pages);
recyclerView.setAdapter(customAdapter);
recyclerView.setLayoutManager(new
LinearLayoutManager(MainActivity.this));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1){
recreate();
}
}
void storeDataInArrays(){
Cursor cursor = myDB.readAllData();
if(cursor.getCount() == 0){
empty_imageview.setVisibility(View.VISIBLE);
no_data.setVisibility(View.VISIBLE);
}else{
while (cursor.moveToNext()){
book_id.add(cursor.getString(0));
book_title.add(cursor.getString(1));
book_author.add(cursor.getString(2));
book_pages.add(cursor.getString(3));
}
empty_imageview.setVisibility(View.GONE);
no_data.setVisibility(View.GONE);
}

Dept of CSE, JIT 2021-2022 17


BOOK LIBRARY

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.delete_all){
confirmDialog();
}
return super.onOptionsItemSelected(item);
}
void confirmDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete All?");
builder.setMessage("Are you sure you want to delete all Data?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i) {
MyDatabaseHelper myDB = new MyDatabaseHelper (MainActivity.this);
myDB.deleteAllData();
//Refresh Activity
Intent intent = new Intent(MainActivity.this,
MainActivity.class);
startActivity(intent);
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.create().show();
}
}

AddActivity.java:

package com.example.Library.MadProject;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AddActivity extends AppCompatActivity implements
View.OnClickListener{
EditText title_input, author_input, pages_input;
Button add_button,View_button;
@Override

Dept of CSE, JIT 2021-2022 18


BOOK LIBRARY

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);

title_input = findViewById(R.id.title_input);
author_input = findViewById(R.id.author_input);
pages_input = findViewById(R.id.pages_input);
View_button=findViewById(R.id.btnView);
View_button.setOnClickListener(this);
add_button = findViewById(R.id.add_button);
add_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MyDatabaseHelper myDB = new MyDatabaseHelper
(AddActivity.this);
myDB.addBook(title_input.getText().toString().trim(),
author_input.getText().toString().trim(),
Integer.valueOf(pages_input.getText().toString().trim()));
}
});
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
}

UpdateActivity.java:

package com.example.Library.MadProject;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class UpdateActivity extends AppCompatActivity {


EditText title_input, author_input, pages_input;
Button update_button, delete_button;
String id, title, author, pages;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
title_input = findViewById(R.id.title_input2);
author_input = findViewById(R.id.author_input2);
pages_input = findViewById(R.id.pages_input2);
update_button = findViewById(R.id.update_button);
delete_button = findViewById(R.id.delete_button);

Dept of CSE, JIT 2021-2022 19


BOOK LIBRARY

//First we call this


getAndSetIntentData();
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setTitle(title);
}
update_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//And only then we call this
MyDatabaseHelper myDB = new MyDatabaseHelper
(UpdateActivity.this);
title = title_input.getText().toString().trim();
author = author_input.getText().toString().trim();
pages = pages_input.getText().toString().trim();
myDB.updateData(id, title, author, pages);
}
});
delete_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
confirmDialog();
}
});
}
void getAndSetIntentData(){
if(getIntent().hasExtra("id") && getIntent().hasExtra("title") &&
getIntent().hasExtra("author") &&
getIntent() .hasExtra("pages")){
//Getting Data from Intent
id = getIntent().getStringExtra("id");
title = getIntent().getStringExtra("title");
author = getIntent().getStringExtra("author");
pages = getIntent().getStringExtra("pages");
//Setting Intent Data
title_input.setText(title);
author_input.setText(author);
pages_input.setText(pages);
Log.d("stev", title+" "+author+" "+pages);
}else{
Toast.makeText(this, "No data.", Toast.LENGTH_SHORT).show();
}
}
void confirmDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete " + title + " ?");
builder.setMessage("Are you sure you want to delete"+title + " ?");
builder.setPositiveButton("Yes", new DialogInterface.
OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
MyDatabaseHelper myDB = new MyDatabaseHelper
(UpdateActivity.this);
myDB.deleteOneRow(id);
finish();
}

Dept of CSE, JIT 2021-2022 20


BOOK LIBRARY

});
builder.setNegativeButton("No", new DialogInterface.
OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.create().show();
}
}

CustomAdapter.java:

package com.example.Library.MadProject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;
import com.example.Library.MadProject.R;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder>
{
private Context context;
private Activity activity;
private ArrayList book_id, book_title, book_author, book_pages;

CustomAdapter(Activity activity, Context context, ArrayList book_id, ArrayList


book_title, ArrayList book_author,
ArrayList book_pages){
this.activity = activity;
this.context = context;
this.book_id = book_id;
this.book_title = book_title;
this.book_author = book_author;
this.book_pages = book_pages;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.my_row, parent, false);
return new MyViewHolder(view);
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder,
@SuppressLint("RecyclerView") final int position) {

Dept of CSE, JIT 2021-2022 21


BOOK LIBRARY

holder.book_id_txt.setText(String.valueOf(book_id.get(position)));
holder.book_title_txt.setText(String.valueOf(book_title.get(position)));
holder.book_author_txt.setText(String.valueOf(book_author.get(position)));
holder.book_pages_txt.setText(String.valueOf(book_pages.get(position)));
//Recyclerview onClickListener
holder.mainLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, UpdateActivity.class);
intent.putExtra("id", String.valueOf(book_id.get(position)));
intent.putExtra("title", String.valueOf(book_title.get(position)));
intent.putExtra("author",
String.valueOf(book_author.get(position)) );
intent.putExtra("pages", String.valueOf(book_pages.get(position)));
activity.startActivityForResult(intent, 1);
}
});
}
@Override
public int getItemCount() {
return book_id.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView book_id_txt, book_title_txt, book_author_txt, book_pages_txt;
LinearLayout mainLayout;
MyViewHolder(@NonNull View itemView) {
super(itemView);
book_id_txt = itemView.findViewById(R.id.book_id_txt);
book_title_txt = itemView.findViewById(R.id.book_title_txt);
book_author_txt = itemView.findViewById(R.id.book_author_txt);
book_pages_txt = itemView.findViewById(R.id.book_pages_txt);
mainLayout = itemView.findViewById(R.id.mainLayout);
//Animate Recyclerview
Animation translate_anim = AnimationUtils.loadAnimation(context,R.anim.
translate_anim);
mainLayout.setAnimation(translate_anim);
}
}
}

MyDatabaseHelper.java:

package com.example.Library.MadProject;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import androidx.annotation.Nullable;
public class MyDatabaseHelper extends SQLiteOpenHelper {
private Context context;
private static final String DATABASE_NAME = "BookLibrary.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "my_library";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TITLE = "book_title";
private static final String COLUMN_AUTHOR = "book_author";
private static final String COLUMN_PAGES = "book_pages";
MyDatabaseHelper(@Nullable Context context) {

Dept of CSE, JIT 2021-2022 22


BOOK LIBRARY

super(context, DATABASE_NAME, null, DATABASE_VERSION);


this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_TITLE + " TEXT, " +
COLUMN_AUTHOR + " TEXT, " +
COLUMN_PAGES + " INTEGER);";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
void addBook(String title, String author, int pages){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();

cv.put(COLUMN_TITLE, title);
cv.put(COLUMN_AUTHOR, author);
cv.put(COLUMN_PAGES, pages);
long result = db.insert(TABLE_NAME,null, cv);
if(result == -1){
Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context, "Added Successfully!",
Toast.LENGTH_SHORT).show();
}
}
Cursor readAllData(){
String query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = null;


if(db != null){
cursor = db.rawQuery(query, null);
}
return cursor;
}
void updateData(String row_id, String title, String author, String pages){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, title);
cv.put(COLUMN_AUTHOR, author);
cv.put(COLUMN_PAGES, pages);
long result = db.update(TABLE_NAME, cv, "_id=?", new String[]{row_id});
if(result == -1){
Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context, "Updated Successfully!",
Toast.LENGTH_SHORT) .show();
}
}
void deleteOneRow(String row_id){
SQLiteDatabase db = this.getWritableDatabase();
long result = db.delete(TABLE_NAME, "_id=?", new String[]{row_id});
if(result == -1){
Toast.makeText(context, "Failed to Delete.", Toast.LENGTH_SHORT).show();

Dept of CSE, JIT 2021-2022 23


BOOK LIBRARY

}else{
Toast.makeText(context, "Successfully Deleted.",
Toast.LENGTH_SHORT).show();
}
}

void deleteAllData(){
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME);
}
}

Dept of CSE, JIT 2021-2022 24


CHAPTER 4

Screen Shots
BOOK LIBRARY

Screenshots:

Figure 4.1:Home Page Figure 4.2:Add and View Page

Dept of CSE, JIT 2021-2022 26


BOOK LIBRARY

Figure 4.3: Successful Insertion Figure 4.4: Book List Page

Dept of CSE, JIT 2021-2022 27


BOOK LIBRARY

Figure 4.5: Update and Delete Page Figure 4.6: Successful Updation

Dept of CSE, JIT 2021-2022 28


BOOK LIBRARY

Figure 4.7: Successful Deletion Figure 4.8: List after Deletion

Dept of CSE, JIT 2021-2022 29

You might also like