Search code examples
databaseandroid-studiokotlinandroid-fragmentsandroid-room

How to increase the value of a text by my database


I want to change the value of a variable called "value" which represent the shown value in the screen according to my database, here is my code

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()
            })
        }

please I need a useful answer and thanks for your patience


Solution

  • You need to perform an update query. Using ROOM for your SQLITE, you create the update query on your DAO class, you can call RestaurantDao. Allocate all your queries there such as update, delete, insert, etc.

    Example: (modify the names of the table and variables according to your table structure.

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