Intermediate 30 min Java Full project

RecyclerView with SQLite in Android Studio (Java)

Save users to a SQLite database and show them in a scrolling RecyclerView list. This is the pattern behind almost every list screen you will ever build.

What you'll learn

  • Store records with SQLiteOpenHelper
  • Build a RecyclerView adapter and ViewHolder
  • Load database rows into the list
  • Open a second screen to show the list

Before you start

  • Android Studio (any recent version)
  • Basic Java: variables, methods and if/else
  • An emulator or an Android phone

How it fits together

  1. 1Form
  2. 2DBHelper
  3. 3Userdetails table
  4. 4List of User
  5. 5MyAdapter
  6. 6RecyclerView cards

Build it step by step

1. Create the project

In Android Studio choose New Project → Empty Views Activity, with Language Java and Minimum SDK API 24. RecyclerView comes with the Material library, so there's nothing extra to add.

2. Build the form

In activity_main.xml, add three boxes (name, e-mail, age) and three buttons: Insert, Delete and View.

3. Create the database helper

DBHelper extends SQLiteOpenHelper and creates a Userdetails table. name is the primary key, so each name can be saved once.

4. Add a User class

A small class holding one user's name, e-mail and age. Passing User objects around is much simpler than keeping three separate lists in step.

5. Read users as a list

getdata() runs a query, turns each row into a User, and closes the cursor. The screens never touch the cursor.

6. Design one row

userentry.xml is one card: name, e-mail, age and a delete icon. RecyclerView reuses this layout for every row.

7. Write the adapter

MyAdapter does two jobs:

  • onCreateViewHolder inflates a card.
  • onBindViewHolder fills it with one user's details.

The ViewHolder finds the views once, so scrolling stays smooth.

8. Show the list

In Userlist, load the users, give them to the adapter, and set a LinearLayoutManager so the cards stack vertically. If there are none, show a friendly message instead.

9. Delete from the list

Tapping a card's delete icon removes the user from the database, then calls notifyItemRemoved so the card slides away.

10. Run it

Add three users, tap View to see the cards, then delete one.

If something goes wrong

  • The list is empty: check that your inserts succeeded; the toast says so.
  • The app crashes on View: add the Userlist activity to AndroidManifest.xml.
  • Every row shows the same data: fill the views in onBindViewHolder using position, not in the ViewHolder.

What's improved over the video

A User model instead of three parallel lists, cards with a delete button on each row, an empty-list message, and cursors that are always closed.

Application code

Select a file to see its code and where it belongs.

This code uses package com.example.recyclerviewsqlite. Change that first line to your own package, the one at the top of your MainActivity.

Replace fileapp/src/main/java/com/example/recyclerviewsqlite/MainActivity.java

The form: insert, delete by name, or open the list.

MainActivity.java
package com.example.recyclerviewsqlite;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

/** The form: add a user, delete one by name, or open the list. */
public class MainActivity extends AppCompatActivity {
    EditText name, email, age;
    Button insert, delete, view;
    DBHelper DB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Screens.setUp(this, R.layout.activity_main);

        name = findViewById(R.id.name);
        email = findViewById(R.id.email);
        age = findViewById(R.id.age);
        insert = findViewById(R.id.btnInsert);
        delete = findViewById(R.id.btnDelete);
        view = findViewById(R.id.btnView);
        DB = new DBHelper(this);

        view.setOnClickListener(v -> startActivity(new Intent(this, Userlist.class)));

        insert.setOnClickListener(v -> {
            String nameTXT = name.getText().toString().trim();
            if (nameTXT.isEmpty()) {
                Toast.makeText(this, R.string.need_name, Toast.LENGTH_SHORT).show();
                return;
            }
            boolean added = DB.insertuserdata(nameTXT, email.getText().toString().trim(), age.getText().toString().trim());
            Toast.makeText(this, added ? R.string.inserted : R.string.not_inserted, Toast.LENGTH_SHORT).show();
            if (added) {
                name.setText("");
                email.setText("");
                age.setText("");
            }
        });

        delete.setOnClickListener(v -> {
            String nameTXT = name.getText().toString().trim();
            boolean removed = DB.deleteuserdata(nameTXT);
            Toast.makeText(this, removed ? getString(R.string.deleted, nameTXT) : getString(R.string.not_deleted), Toast.LENGTH_SHORT).show();
        });
    }
}

Full source code