0

Can't get where is my mistake - it seems data is inserted (I checked the database file through Device File Explorer) but it doesn't returns. I wonder whether it's in Adapter or ViewHolder or anywhere else. Any help is granted!

This the activity where I perform my queries

public class ShowDatabaseActivity extends AppCompatActivity {
    private List <Contact> contactsList = new ArrayList<>()

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_database);
        setupToolbar();
        initRecyclerView();
        Intent intent = getIntent();
        unpack(intent);
    }

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

    private void initRecyclerView() {
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        final ContactsListAdapter adapter = new ContactsListAdapter(contactsList);
        adapter.notifyDataSetChanged();
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);
    }

    private void unpack(Intent intent) {
        final Handler handler = new Handler();
        Thread backgroundThread = new Thread(() -> {
            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);
            AppDatabase.getINSTANCE(ShowDatabaseActivity.this).contactDao().insert(contact);
            AppDatabase.getINSTANCE(ShowDatabaseActivity.this).contactDao().getAll();
            handler.post(() -> {

            });
        });
        backgroundThread.start();
    }
}

If I debug this line shows data sucessfully -

(ShowDatabaseActivity.this).contactDao().insert(contact);

My adapter

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

    public ContactsListAdapter(@NonNull List<Contact> contacts) {
        this.contacts = contacts;
    }

    @Override
    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);
    }

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

    @Override
    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) {
            super(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);
        }
    }

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

My DataBase

@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;
    }

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

There is no doubt in my Entity and Dao classes as it worked perfectly with other variant of database so I don't attach them. Will be very grateful for any help!

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
  • Your handler.post have nothing inside, and you are not setting any value inside your adapter also, you need to make your adapter object global for the class and need to create a method inside your Adapter to add data inside the list and notify the chnages accordingly – Rakshit Nawani Feb 20 '20 at 11:49
  • Thank you for your comment. Is handler.post() important? I thought it posts changes to UI - and I want to perform queries on the background. Sorry for asking - I'm new to Android –  Feb 20 '20 at 11:58

2 Answers2

0
 AppDatabase.getINSTANCE(ShowDatabaseActivity.this).contactDao().getAll();

This code does not assign data to anything/ variable to hold your data. Assign data to a reference variable and pass that variable to the adapter so it can show data from it.

Lakhwinder Singh
  • 5,247
  • 3
  • 20
  • 36
0

Your code is incomplete

You are setting only empty Array inside your adapter, to rectify this make your Adapter's object global

Your handler.post have nothing inside it

you need to create a function inside you adapter like below

      public void addItem(List<Contacts> list) {

        mList.add(list) 

 notifyItemInserted(mList.size - 1)

    }

Now you need to call addItem inside your handler.post by using

adapter.addItem(contact)

This will add the content inside your adapter's list and notify the chnages also

Rakshit Nawani
  • 2,490
  • 12
  • 25