【问题标题】:How to increase the value of a text by my database如何通过我的数据库增加文本的值
【发布时间】:2022-01-18 21:46:21
【问题描述】:

我想根据我的数据库更改一个名为“value”的变量的值,它代表屏幕中显示的值,这是我的代码

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    val mViewModel = ViewModelProvider(this).get(ExpenseCalculatorViewModel::class.java)
    binding.categoryName.text = args.category
    val viewModel = ViewModelProvider(this).get(CategoriesViewModel::class.java)
    when (binding.categoryName.text) {
        getString(R.string.restaurant) -> {
            viewModel.readRestaurantData.observe(viewLifecycleOwner, {
                var value = 0
                for (i in it.indices) {
                    value += it[i].restaurant
                }
                binding.totalNumber.text = value.toString()
            })
        }

我需要一个有用的答案,感谢您的耐心等待

【问题讨论】:

  • 您能否澄清更改后的值会发生什么?你想在屏幕上显示还是保存在数据库中?

标签: database android-studio kotlin android-fragments android-room


【解决方案1】:

您需要执行更新查询。为您的 SQLITE 使用 ROOM,您可以在 DAO 类上创建更新查询,您可以调用 RestaurantDao。在那里分配所有查询,例如更新、删除、插入等。

示例:(根据你的表结构修改表名和变量名。

@Query("UPDATE my_restaurant_table SET value=:value " +    
        "WHERE id=:id"
) public abstract void updateRestaurant(
        long id,
        String value
);

【讨论】:

    猜你喜欢
    • 2019-08-21
    • 2011-10-16
    • 2015-11-04
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多