简体   繁体   English

插入数据后,会议室数据库表仍为空

[英]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. 我刚刚完成了有关Room Database的代码实验室课程,但似乎无法将数据加载到表中。

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. 我的项目在recyclerview中显示了一堆项目,并且当用户在recyclerview中按下其中一个项目的按钮时,它打算被添加到Room数据库中创建的表中。

I've opened the Room db using sqlite db explorer with the files from the device explorer. 我已经使用sqlite db资源管理器打开了Room db,并将设备资源管理器中的文件打开了。 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 这是从onBindViewHolder为我的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 这是用于填充数据库的AsyncTask,也在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

@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. 我已经检查了此适配器,列表大小为0。

If you need anymore info please do ask, thanks for your time/ 如果您需要更多信息,请务必询问,谢谢您的时间/

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

You are creating an AsyncTask . 您正在创建一个AsyncTask You are never executing it, so your doInBackground() is not being run. 您永远不会执行它,因此不会运行doInBackground() You could test this by putting a breakpoint in doInBackground() or adding logging to doInBackground() . 您可以通过在doInBackground()放置一个断点或将日志添加到doInBackground()来进行doInBackground()

To fix it, execute() your AsyncTask . 要修复它,请execute()您的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.). 另外,请注意,由于您未使用onPostExecute() ,因此与使用任何其他线程解决方案( ThreadExecutor ,RxJava等)相比,此处使用AsyncTask价值很小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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