Welcome to EasyCoding with Ammara
Subscribe My Coding Channel:
https://www.youtube.com/channel/UC8UsfNYmbKiRJvI9ZhhApEw?view_as=subscriber
Subscribe my personal YouTube Channel:
https://www.youtube.com/channel/UCa93R2oNQMuX0fOKa9eV4zg?view_as=subscriber
Let's Start
Android SQLite Database Tutorial:AOA All, In this Post, I am going to show you how to get email and password from user during registration and then store to sqlite database. After Store in database, I show your how to get data from database and show in recyclerview. After this, I also show how to update and delete data in database. So let's watch video. EasyCoding with Ammara:
Watch My full video on SQLite Database to understand completely:
https://www.youtube.com/watch?v=MTxzaemF0G4&list=PLTR7MDYHvzpO-yqF88i9xy_I_5dl2HXHa
1) Add Code to MainActivity.
2) Add Code to activity_recycler_view.xml.
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=".RecyclerViewActivity">
android:layout_width="match_parent"
android:id="@+id/recyclerview"
android:layout_height="match_parent">
3) Create layout for recyclerview.
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
android:layout_width="match_parent"
android:layout_margin="5dp"
android:padding="5dp"
android:layout_height="wrap_content">
android:layout_width="wrap_content"
android:text="Id is : "
android:textSize="15dp"
android:textStyle="bold"
android:layout_height="wrap_content" />
android:layout_width="match_parent"
android:id="@+id/id"
android:layout_height="wrap_content" />
android:layout_width="match_parent"
android:layout_margin="5dp"
android:padding="5dp"
android:layout_height="wrap_content">
android:layout_width="wrap_content"
android:text="Email is : "
android:textSize="15dp"
android:textStyle="bold"
android:layout_height="wrap_content" />
android:layout_width="match_parent"
android:id="@+id/email"
android:layout_height="wrap_content" />
android:layout_width="match_parent"
android:layout_margin="5dp"
android:padding="5dp"
android:layout_height="wrap_content">
android:layout_width="wrap_content"
android:text="Pass is : "
android:textSize="15dp"
android:textStyle="bold"
android:layout_height="wrap_content" />
android:layout_width="match_parent"
android:id="@+id/pass"
android:layout_height="wrap_content" />
android:layout_width="match_parent"
android:text=".............."
android:layout_gravity="center"
android:textStyle="bold"
android:textColor="@color/colorPrimary"
android:gravity="center"
android:layout_height="wrap_content" />
4) Create layout for Custom Dialog Box.
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:text="Update Dialog Box"
android:textColor="@color/colorPrimary"
android:textStyle="bold"
android:textSize="19dp"
android:gravity="center"
android:padding="5dp"
android:layout_margin="5dp"
android:layout_height="wrap_content" />
android:layout_width="match_parent"
android:id="@+id/update"
android:hint="Enter Email"
android:layout_margin="5dp"
android:layout_height="wrap_content" />
5)Create Model Class for get and set data.
package com.example.sqlitedatabase;
public class Data {
int id;
String email;
String pass;
public Data(){
}
//Create table (query)
public static final String CREATE_TABLE=
"CREATE TABLE " + "Hello" + "("+ "Id" + " INTEGER PRIMARY KEY AUTOINCREMENT," +
"Email" + "TexT," +
"Password" + "TexT"+ ")";
public Data(int id, String email, String pass) {
this.id = id;
this.email = email;
this.pass = pass;
}
public int getId() {
return id;
}
public Void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
}
6 ) Create Adapter for Recyclerview (NotesAdapter).
package com.example.sqlitedatabase;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class NotesAdapter extends RecyclerView.Adapter {
private Context context;
private List notesList;
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.data_recycler, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Data note = notesList.get(position);
holder.email.setText(note.getEmail());
holder.id.setText( Integer.toString(note.getId()));
holder.pass.setText(note.getPass());
}
public NotesAdapter(Context context, List notesList) {
this.context = context;
this.notesList = notesList;
}
@Override
public int getItemCount() {
return notesList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView id;
public TextView email;
public TextView pass;
public MyViewHolder(View view) {
super(view);
id = view.findViewById(R.id.id);
email = view.findViewById(R.id.email);
pass = view.findViewById(R.id.pass);
}
}
}
7) Create DatabaseHelper class for store data to db.
package com.example.sqlitedatabase;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Hello";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(Data.CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long insertData(String email,String pass){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("Email",email);
values.put("Password",pass);
long id=db.insert("Hello",null,values);
db.close();
return id;
}
public List getAllDataFromDb(){
List notes = new ArrayList();
String selectQuery = "SELECT * FROM " + "Hello" ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Data note = new Data();
note.setId(cursor.getInt(cursor.getColumnIndex("Id")));
note.setEmail(cursor.getString(cursor.getColumnIndex("Email")));
note.setPass(cursor.getString(cursor.getColumnIndex("Password")));
notes.add(note);
} while (cursor.moveToNext());
}
db.close();
return notes;
}
}
8) Paste code to main Activity.
package com.example.sqlitedatabase;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText email,pass;
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=new DatabaseHelper(this);
//getting id
email=findViewById(R.id.email);
pass=findViewById(R.id.password);
}
public void Save(View view){
String saveEmail=email.getText().toString();
String savePass=pass.getText().toString();
db.insertData(saveEmail,savePass);
Intent intent=new Intent(MainActivity.this,RecyclerViewActivity.class);
startActivity(intent);
}
}
9) Paste code to RecyclerViewActivity.java.
package com.example.sqlitedatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewActivity extends AppCompatActivity {
private List notesList = new ArrayList();
private RecyclerView recyclerView;
DatabaseHelper db;
NotesAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
recyclerView = findViewById(R.id.recyclerview);
//where you use object of databasehelper (db)
//you have to initialize it.
db = new DatabaseHelper(this);
//get all data from database and store in list (noteslist)
//then pass to recyclerview.
notesList.addAll(db.getAllDataFromDb());
Log.e("xxxxxxxxxxxxxxx",notesList.get(0).getEmail());
mAdapter = new NotesAdapter(this, notesList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}
}
10) Drawable:
loginbg.xml:
android:color="@color/colorPrimary"
android:width="1dp" />