插入数据后,会议室数据库表仍为空 [英] Room Database Table is still empty after inserting data

查看:80
本文介绍了插入数据后,会议室数据库表仍为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了Room Database的代码实验室课程,但似乎无法将数据加载到表中.

我已将课程应用于我的项目.我的项目在recyclerview中显示了一堆项目,当用户按下recyclerview中一个项目的按钮时,它打算被添加到Room数据库中创建的表中.

我已经使用sqlite db资源管理器打开了Room数据库,其中包含设备资源管理器中的文件.我可以看到数据库以及要插入的表均已创建,但表中没有数据.

这是来自onBindViewHolder,用于我的recyclerview

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

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

我已经检查并确保产品不为空.

这是用于填充数据库的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;
        }
    }

这是我的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();
}

我正在尝试在购物车片段中显示此表数据,并且我正在使用此方法在数据准备好后显示它.

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

我已经检查了此适配器,列表大小为0.

如果您需要更多信息,请务必询问, 谢谢您的时间/

解决方案

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

您正在创建一个AsyncTask.您从不执行它,因此您的doInBackground()未被运行.您可以通过在doInBackground()中放置断点或将日志记录添加到doInBackground()中来对此进行测试.

要修复此问题,请execute()您的AsyncTask.

此外,请注意,由于您未使用onPostExecute(),因此与使用任何其他线程解决方案(ThreadExecutor,RxJava等)相比,此处使用AsyncTask的价值很小./p>

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.).

这篇关于插入数据后,会议室数据库表仍为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆