简体   繁体   中英

Room Database Table is still empty after inserting data

I've just finished the codelab course on Room Database but I can't seem to get the data loaded into the table.

I've applied the course to my project. My project displays a bunch of items in a recyclerview and when a user pushed a button on one of the items in the recyclerview , it's meant to get added to the table created in the Room database.

I've opened the Room db using sqlite db explorer with the files from the device explorer. I can see the database has been created as well as the table I'm trying to insert into, yet theres no data in the table.

This is from onBindViewHolder for my recyclerview

holder.QtyAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //Code to build product
                new PopulateDB(RoomDatabase.getDatabase(context), product);
            }
        });

I've checked and ensured product isn't null.

This is the AsyncTask used to populate the DB, also in the adpater

private static class PopulateDB extends AsyncTask<Void,Void,Void>{

        private final CartDao cartDao;
        private final Product product;

        PopulateDB(RoomDatabase database, Product product){
            cartDao = database.CartDao();
            this.product = product;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            cartDao.insert(product);
            return null;
        }
    }

Here is my DAO

@Dao
public interface CartDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(Product productCart);

    @Query("DELETE FROM promotion_cart")
    void clearCart();

    @Query("SELECT * from promotion_cart")
    LiveData<List<PromotionProduct>> getAllPromotionCartItems();
}

I'm trying to show this table data in a cart fragment, and I'm using this method to show the data once it's ready.

cartViewModel.getAllCartItems().observe(this, new Observer<List<Product>>() {
            @Override
            public void onChanged(@Nullable List<Product> products) {
                cartAdapter.setProductCartList(products);
            }
        });

I've checked this adapter and the list size is 0.

If you need anymore info please do ask, thanks for your time/

new PopulateDB(RoomDatabase.getDatabase(context), product);

You are creating an AsyncTask . You are never executing it, so your doInBackground() is not being run. You could test this by putting a breakpoint in doInBackground() or adding logging to doInBackground() .

To fix it, execute() your AsyncTask .

Also, note that since you are not using onPostExecute() , there is little value in using AsyncTask here, compared to using any other threading solution ( Thread , Executor , RxJava, etc.).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM