Home » Android » android – Room – Cannot display database's full list-Exceptionshub

android – Room – Cannot display database's full list-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment


I’m new to Android and Room and still can’t get the full sense of it – will be grateful for any help. The database is created and all queries work, the data is inserted in the table (as shown by Stetho). The problem is that I cannot display all the table’s contents in my activity – it shows only last insertion.
Here is the activity where I perform queries

public class ShowDatabaseActivity extends AppCompatActivity {
    private List<Contact> allContacts = new ArrayList<>();
    public ContactsListAdapter adapter;
    private ContactDao contactDao;

    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();

    private void setupToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        if (getSupportActionBar() != null) {
            toolbar.setNavigationOnClickListener(v -> onBackPressed());

    private void initRecyclerView() {
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        adapter = new ContactsListAdapter(allContacts, ShowDatabaseActivity.this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

    private void unpack(Intent intent) {
        Bundle extras = intent.getExtras();
        String lastName = extras.getString(Constants.LAST_NAME_KEY);
        String firstName = extras.getString(Constants.FIRST_NAME_KEY);
        String middleName = extras.getString(Constants.MIDDLE_NAME_KEY);
        int age = extras.getInt(Constants.AGE_KEY);
        Contact contact = new Contact(lastName, firstName, middleName, age);
        final Handler handler = new Handler();
        Thread backgroundThread = new Thread(new Runnable() {
            public void run() {
                handler.post(new Runnable() {
                    public void run() {

And my Adaper

public class ContactsListAdapter extends RecyclerView.Adapter<ContactsListAdapter.ContactViewHolder> {
    private Context context;
    private List<Contact> contacts;
    private LayoutInflater inflater;

    public ContactsListAdapter(@NonNull List<Contact> contacts, Context context) {
        this.contacts = contacts;
        inflater = LayoutInflater.from(context);
        this.context = context;

    public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        View itemView = inflater.inflate(R.layout.recycler_view, parent, false);
        return new ContactViewHolder(itemView);

    public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) {
        Contact currentContact = contacts.get(position);
        if (currentContact != null) {
        } else {
            holder.contactLastNameView.setText("No information");
            holder.contactFirstNameView.setText("No information");
            holder.contactMiddleNameView.setText("No information");
            holder.contactAgeView.setText("No information");

    public int getItemCount() {
        return contacts.size();

    class ContactViewHolder extends RecyclerView.ViewHolder {
        private final TextView contactLastNameView;
        private final TextView contactFirstNameView;
        private final TextView contactMiddleNameView;
        private final TextView contactAgeView;

        private ContactViewHolder(View itemView) {
            contactLastNameView = itemView.findViewById(R.id.last_name_text_view);
            contactFirstNameView = itemView.findViewById(R.id.first_name_text_view);
            contactMiddleNameView = itemView.findViewById(R.id.middle_name_text_view);
            contactAgeView = itemView.findViewById(R.id.age_text_view);

    public void addItem(Contact contact) {
        notifyItemInserted(contacts.size() - 1);

    public int getItemViewType(final int position) {
        return R.layout.recycler_view;

And my DataBase

package com.example.sqliteorm;
import android.content.Context;
import android.util.Log;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import java.util.List;

@Database(entities = {Contact.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
    public abstract ContactDao contactDao();

    private List<Contact> allContacts;

    List<Contact> getAllContacts() {
        return allContacts;

    private static AppDatabase INSTANCE;

    public synchronized static AppDatabase getINSTANCE(Context context) {
        INSTANCE = getDatabase(context);
        return INSTANCE;

    static AppDatabase getDatabase(final Context context) {
        if (INSTANCE == null) {
            synchronized (AppDatabase.class) {
                INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                        AppDatabase.class, "table_contacts")
                Log.d("LOG", "Getting the database instance");
        return INSTANCE;

My Constants Class

    public final static String LAST_NAME_KEY = "LAST_NAME_KEY";
    public final static String FIRST_NAME_KEY = "FIRST_NAME_KEY";
    public final static String MIDDLE_NAME_KEY = "MIDDLE_NAME_KEY";
    public final static String AGE_KEY = "AGE_KEY";

    private Constants() {
My queries seem to be good as I used them in different version of database - so do not post them
 I will greatly appreciate any help!
How to&Answers: