You are on page 1of 34

you will see a mainactivity.java and activity underscore main.

xml has opened now go to the code and


you will see layout to relative layout and also since it is a relative layout we have to

write where it is placed exactly so we have to write layout below id text then hint for that edit text we
can write

name now we'll add another text

and which will be match parent and

height will be wrap content id of that

edit text will be id slash email

then size same as 24 dp and it should be

placed below the uh id

name

then hint of that edit text box will be

email

then we will add another edit text box

so which will match parent and height it

will be app content id will be idh

so here you will ask a user to put age

text size will be 24 db layout below it

is email

hint will be

age and now we will create some buttons

so button width will be match parent

height will be wrap content

id of the button will button insert

then text on the button will be insert

data and size will be 24 db slightly

bigger

and it will be placed below the last

edit text block page so accordingly id


has to be set margin top i'm giving it

20 dp

then copy this code and paste one more

button here and

change the id to button view and text tview data and it should be placed below

button insert

so now you can see the main ui is ready

and two buttons and three edit box has

come

so go to maintenance.java

create variables edittext

name comma email comma age

now button

insert

comma view two buttons we are

declaring here now we have to initialize

them name equal to find v by id r dot id

dot name

email equal to find it by id or dot id

dot email similarly h equal to find v by

id r dot id dot h

now we will initialize buttons insert

equal to find u by id r dot id dot

button insert view equal to find u by id

r dot are your button view

now we'll create a listener for the

button so view dot set on click listener

new view on click listener rest of the

things will automatically come


now we will write start activity new

intent

main activity dot disk

comma user list dot class

we have not created this usually just

class so now we will create that

go to new create an empty activity

and give the name whatever name you are

given user list

so

you can see the error is gone

now create one more java class

where it is db helper because we have to

now use sql database so now extend this

to escalate open helper

now click here and implement method so

select two and click ok

now create the constructor

delete this nullable and other rest of

the things

also the inside the super

change the name as userdata.db that's

our name of the database then factory

null and version equal to 1. you can

choose accordingly but

now under the oncreate i'm just changing

the db to capital db just for the sake


of representation and in on upgrade also

i will change the names

then int i and into

new version double i then inside the

oncreate i will write db dot exact sql

create table

user details that's the table name we

are creating so

name will be our text primary key

comma email text and age text so here

you can see name is our primary key by

which it will be actually

attached

so name cannot be duplicated here and

email and h can be duplicated now on the

on upgrade method we will write db dot

exec sql drop table if exists

user details

now we will create a public boolean

insert user data

5:49

inside that it will ask for the

5:51

arguments string name comma string email

5:54

comma

5:55

string age
5:57

so insert the function sqlite database

5:59

db equal to this dot

6:02

get write table database now we will

6:03

write content values

6:06

content values equal to new content

6:09

values

6:10

then content values dot put

6:13

then under the key name comma name

6:16

copy this and repeat two times and

6:18

change the name that email email then

6:21

the third is also age age what this will

6:24

do this will put the the string name

6:26

whatever you pass to this function

6:28

on the database and will put that into

6:31
the name attribute then email will be

6:34

passed to email attribute and similarly

6:36

age will be passed to age attribute now

6:37

it is time to check the results whether

6:39

it has been inserted or not so long

6:41

result equal to db dot

6:43

insert now we are calling insert

6:45

function insert user details comma null

6:48

comma content values so content values

6:50

will be inserted into the table

6:52

if the results equal to minus 1 that

6:54

means if it has not been inserted

6:56

return false

6:59

else

7:01

return true so these are the basically


7:03

indicators which we will be using in our

7:05

main activity where they are basically

7:07

checked whether the data has been

7:09

inserted or not and accordingly we can

7:10

show the toast message here we have to

7:12

correct result double is equal to

7:14

now we'll create a public cursor

7:17

get data since we want to view data and

7:18

recycler view we have to create a get

7:20

data

7:21

so sqlite database db equal to this

7:25

dot get writeable database

7:27

then cursor cursor equal to eb dot raw

7:30

query

7:32
select star

7:34

from

7:35

user details

7:37

comma

7:38

null so this cursor will actually select

7:40

from user details so

7:42

it will select everything because i am

7:44

not writing any condition in this like

7:46

any update function or if you are

7:48

making a delete function then we have to

7:49

add condition that which particular row

7:50

we want to collect but here we want

7:52

because we want to view all the data we

7:54

have to select everything so we are not

7:56

writing any condition so when we are not


7:57

writing the cursor is selecting the

7:59

entire table entire table will will be

8:01

passed into the cursor variable okay

8:04

now return

8:06

cursor

8:07

that is our get data function so our db

8:09

helper is

8:10

complete we will go to main equity.java

8:14

and we will add our dbhelper

8:16

db

8:18

now we will initialize db equal to new

8:22

dbhelper this

8:27

now we'll create a insert function to

8:30

interface with the database so insert

8:32
dot set on click listener new view one

8:33

click listener

8:36

then inside the

8:37

listener on click method we will write

8:38

string name text equal to

8:41

name dot

8:42

get text dot to string so what whatever

8:45

is a name is given in the edit text box

8:47

will be passed to the name string copy

8:49

this and

8:50

create

8:51

two more strings

8:53

so the name of the another string will

8:55

be email text

8:58

and accordingly the third one will be


9:00

the age

9:02

now write boolean check insert data so

9:05

this is a boolean function which is

9:06

checking that whether insert data has

9:08

been successfully done or not done so

9:11

check answer data equal to db dot insert

9:13

user data pass the argument's name text

9:15

comma email text comma age text now

9:18

write if

9:19

check insert data

9:21

is equal to true

9:23

so a toast message

9:26

write main activity dot this

9:28

and new entry inserted

9:30
else

9:33

if it is not true then create another

9:34

toast new entry not inserted so this is

9:37

how we insert the data now we'll just

9:39

run the program and we'll see

9:41

so our gui is showing properly and when

9:44

we write the just writer name all coding

9:46

and all at the right gmail.com

9:51

then i write age some age you can choose

9:53

30 and when i insert data

9:56

it is

9:57

showing that entry inserted

10:02

the blanks also it is taking since your

10:04

name blank values is able to take you

10:06

can add a condition as per your choice


10:09

if you want some condition that name

10:10

should not be empty you can add the

10:11

condition now if you press again in the

10:14

data it is not taking it because the

10:15

name is already inserted the name it is

10:18

checking with the name attribute that

10:19

name is our primary key is already

10:21

filled with the blank space so it is not

10:23

allowing you to another insert another

10:25

data on that particular name because

10:27

that is for primary key so that's why it

10:28

is showing the toast message new entry

10:30

not inserted now when we press the view

10:32

data you can see the blank space because

10:34
we have not created any recyclerview

10:36

activity this will create now we come

10:38

back to the main page and let us go to

10:40

the layout

10:43

and go to the user list dot xml go to

10:45

the code so this is the our second

10:47

activity where we are going to see

10:48

recycler view so write recycler inside

10:50

that

10:51

here you have to see that reject

10:52

recycler view is automatically coming if

10:54

it is not coming you have to add some

10:55

dependencies but here in this version it

10:58

is taking so

11:00

recyclerview.widget.recyclerview has
11:01

appeared

11:02

now it will be matched parent and might

11:03

also will be match parent and close it

11:05

here and id i am allotting here id

11:08

recyclerview

11:10

so you can see in our

11:12

design it has come now i go to

11:14

userlist.java because i have to work

11:16

with recyclerview i have to make an

11:18

adapter for recyclerviewfirst then that

11:20

adapter i can use in the user list so

11:23

first of all let us create an adapter

11:25

create a new java class my adapter

11:29

extends recycler view dot adapter

11:33
write my.myviewholder

11:39

now

11:42

private context context

11:48

now write private

11:50

array list

11:54

name id

11:56

comma email id

11:59

comma age id

12:00

here we are not writing uh like

12:02

typically we write a released list here

12:04

we are straight away giving the strings

12:06

in the name error list because we are

12:07

not using any class here we are straight

12:09

away writing the strings name here we

12:11

are not using a class for


12:13

writing the data here in this particular

12:15

example now implement the method and

12:18

select all three and press ok

12:21

and you will see all three method has

12:23

come

12:25

now generate constructor right click and

12:28

then

12:28

select all four okay so you can see all

12:30

four has come

12:32

now this my view holder is going red

12:34

because we have to create the class

12:35

select alt enter and click create class

12:38

myviewholder

12:41

so the class has appeared so public

12:42
class myviewholder again go back to

12:44

myviewholder and click alt enter and you

12:47

will see make myviewholder extend

12:48

android recyclerview widget recyclerview

12:51

holder so select that and you can see it

12:54

is now

12:55

added extend recycler view dot view

12:57

holder you can write it also but create

12:58

a constructor matching super for the

13:00

myvi holder now we can delete this

13:03

my adapter here in the

13:05

oncreateviewholder it can be only

13:08

myviewholder also

13:09

similarly onbindviewholder we can also

13:11

delete the same we will start with


13:13

oncreateviewholder we will write a view

13:15

v equal to layout inflator dot from

13:19

context

13:20

dot

13:22

inflate r dot layout dot user entry

13:26

comma parent

13:29

comma

13:29

fall so this user entry is basically the

13:32

layout for our entries to show that how

13:35

we show the entries this will be our

13:37

card view that we will create later so

13:39

that's why it is showing that it is not

13:40

there right now so this return null we

13:42

will change it to new

13:43
myvi holder inside the bracket v now

13:46

come to the class myvi holder declare

13:49

text view

13:52

name id comma email id comma age id now

13:54

we have to initialize this text view by

13:56

the appropriate ids which will be

13:58

declared in user entry so we have to

13:59

first create user entry layout go to

14:01

layout and go to new layout resource

14:03

file the root element we have to change

14:05

here select

14:06

card view dot widget

14:09

then write the file name user entry

14:10

whatever file name you have given there

14:13

click ok
14:15

now you can see cardview.widget has come

14:18

card elevation equal to 12 db will

14:20

define and card corner radius equal to

14:22

15 db

14:24

you can choose whatever you want

14:27

layout margin will be 16 db from all the

14:29

sides background tint i am selecting

14:31

some gray color so that accordingly you

14:33

can set then inside the layout i am

14:36

creating a linear layout so width will

14:38

be match parent and height will be wrap

14:40

content

14:43

now margin for the layout is 16 dp we

14:46

have a margin to our entries in the card

14:48
and orientation will be vertical you

14:50

know because we have to display entries

14:52

vertically

14:53

now inside that vertical linear layout

14:55

we will create a horizontal linear

14:57

layout now so our text and the text

14:59

which we are setting from the database

15:01

so which will be wrap content height

15:03

will be lab content

15:05

and orientation will be horizontal

15:08

then gravity will be center vertical now

15:10

inside the linear layout we will write

15:12

text view which will be wrap content

15:14

height will be wrap content the text

15:16

will be name
15:18

so you can see the name has appeared in

15:19

the

15:20

right side you can see the design

15:25

now text size equal to 24 dp and text

15:27

color will be black and style i am

15:29

keeping it bold

15:31

that's our text view copy it and paste

15:33

it again and this text view the name

15:36

this will be deleted because here the

15:37

text will be set by the program we have

15:39

to give id here id slash text to name

15:42

this id will use for our user entry

15:44

wherever you are declared

15:46

now

15:48
copy this entire inside horizontal

15:50

linear layout and paste it and you can

15:53

see three times name has come so start

15:55

changing it

16:04

the second linear layout we will change

16:06

the text to email

16:09

and change the id to text email

16:14

everything is same the third linear we

16:16

will we will change the text to age and

16:18

we will change the id text stage

16:21

so that's it i think we are our user

16:24

entry layout is uh complete

16:27

now we can go back to our my my

16:29

adapter.java

16:31

and inside that we can write


16:34

name id equal to item view dot

16:39

find by id r dot id dot now we can write

16:41

our our ids text name

16:44

and select this and paste it

16:50

change it to email id and then text

16:52

email

16:53

then third line age id to text age

16:58

now we go to onbindviewholder and we

17:01

write holder dot name id dot set text

17:05

string value of

17:09

name id dot get position

17:13

then copy this and paste it two times

17:16

again and change the name id to email id

17:20

also inside also name id to email id

17:26
third line name id to age id and

17:28

similarly another

17:30

now this get item count we can use any

17:32

value of our entries so we can choose

17:35

name so return

17:37

or name id dot

17:38

size

17:39

so the size of the name id will start

17:41

counting with using that now we will

17:43

come to user list that's our second

17:44

activity where actually the recycler

17:46

view all everything will be shown using

17:48

the layout user entry which is created

17:51

in the my adapter we will write

17:52

recyclerview recyclerview then we will


17:55

create arraylist

17:57

it will be of string type and will

17:58

declare name email comma edge

18:01

db helper db

18:03

then we will declare myadapter adapter

18:06

now inside the oncreate we will

18:08

initialize them db equal to new db

18:10

helper this

18:13

name equal to new era list

18:15

similarly we write for email newer list

18:18

and age

18:20

i will copy and paste and just change

18:23

the names

18:24

so a release has now been declared so

18:28
recycler view is equal to find it by id

18:30

r dot id dot

18:32

our id was recycler view semicolon

18:35

adapter equal to new my adapter this

18:37

comma

18:38

name email h so our my raptor will take

18:42

this data and will display now recycler

18:44

view dot set adapter adapter recycler

18:48

view dot set layout manager new linear

18:51

layout manager this i will write the

18:53

function display data and create this

18:54

method display data

18:57

now inside the display data we will

18:58

write cursor now it is time to

19:00

[Music]
19:01

select everything from the sqlite data

19:04

whatever is data is there we have we

19:05

want to see entire data so

19:07

this is our getdata method we will call

19:09

here

19:10

db dot get data now if cursor dot get

19:14

count equal to 0 that means if it if

19:16

there is no data we can show a toast

19:18

message to user list dot this

19:21

no entry

19:22

exists

19:25

now return

19:26

otherwise if there is some data in the

19:29

our table then we can write while cursor

19:32
dot move to next so it will check for

19:34

each and every cell actually

19:36

so name dot add cursor dot

19:40

get string column index 0. so in the

19:43

column index 0 it will select then email

19:46

dot

19:47

add cursor dot get

19:49

string write another column index 1 then

19:52

write age dot add cursor get string

19:55

column index 2.

19:57

so this will keep on going to the each

19:59

and every

20:00

new entry

20:02

here the height will be wrap content

20:05

because we have to we do not want to


20:07

stretch it to

20:08

sing one card to stretch the entire ui

20:11

so just correct it here

20:15

that's his friend and now it's time to

20:17

run the project so let's just click run

20:21

and you can see the our ui is come

20:25

and we will first press the view data

20:28

and you can see that our last time we

20:30

put some entries this entry is appearing

20:32

here card is also looking very nice and

20:34

i will enter some data

20:36

new data vbp ppp gmail.com you can

20:39

choose anything and

20:40

age i am just putting 40 here now and

20:42
you can see that when you press the

20:44

insert the toast has come new entry

20:46

inserted and now we go to view you can

20:47

see one more card is appearing very

20:49

nicely

20:51

and the recycler view is working here

20:53

and taking the data from sql database if

20:55

you change this

20:57

new entry not inserted because the data

20:59

has already been

21:00

inserted we are not actually made the

21:03

button for update data it is only view

21:05

data so it is not allowing

21:07

the another attribute for the same name

21:09

which is already created so it is saying


21:11

no and new entry not inserted the update

21:14

and delete is there if you want to if

21:16

you want to know about this there is

21:18

another video of mine

21:19

uh where i am given all the insert

21:22

delete update and all that's it guys the

21:23

project is complete now i think you

21:25

enjoyed this and uh you must have

21:27

learned something from this and so i

21:28

request you to please subscribe and

21:30

press the bell icon for notifications

21:32

and i will keep bringing you nice

21:34

tutorials which will be very helpful for

21:36

you thank you very much

English (auto-generated)
AllFrom your searchSQLiteRelatedLiveRecently uploadedWatched

You might also like