如何从 Android Studio 的数据库中获取卡路里总量?

问题描述 投票:0回答:1

这是 Kotlin 中的代码。

我的DAO:

@Dao
interface DataInputUserDAO {

    // Will ignore if new data is the same
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    // can be started, paused, and resume
    suspend fun addData(dataInput : DataInputUser)

    @Query("DELETE FROM user_table WHERE id is :dataID")
    suspend fun deleteData(dataID: Int)

    @Update
    suspend fun updateData (dataInput: DataInputUser)

    // Select everything from DataInputUser
    @Query("SELECT * FROM user_table ORDER BY id ASC")
    //Return every item in the database
    fun readData() : LiveData<List<DataInputUser>>

    //Sum Carbs
    @Query ("SELECT SUM(amountFood) FROM user_table WHERE typeFood ='Carbs' ")
    fun sumCarbs(): LiveData<Int>

我的数据库

@Parcelize
@Entity(tableName = "user_table")
data class DataInputUser(
    var nameFood: String,
    var typeFood: String,
    var amountFood: Int,
    @PrimaryKey(autoGenerate = true)
    var id : Int
) : Parcelable

我想显示卡路里总量的片段

class ListFragment : Fragment() {

    //creating a private writable property called listBinding
    private var listBinding: FragmentListBinding? = null
    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = listBinding!!
    private lateinit var myUserViewModel : UserViewModel

    // to get the Argument from SafeArg
    private val args by navArgs<UpdateFragmentArgs>()

    // Will be used to get the calories
    // Array list is used so we can sum it
    //private lateinit var amountFoodData : ArrayList<DataInputUser>

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        //creates an instance of the binding class for the fragment to use.
        listBinding = FragmentListBinding.inflate(inflater, container, false)
        val view = binding.root

        //Making the Recycler View
        val myAdapter = adapter()
        val recyclerView = binding.recyclerView
        recyclerView.adapter = myAdapter
        recyclerView.layoutManager = LinearLayoutManager(requireContext())

        // Showing the number
        //updateNumber()

        // ViewModelProvider is a utility class that provides ViewModel instances.
        myUserViewModel = ViewModelProvider(this).get(UserViewModel::class.java)
        // To observe Live Data and Changes from DAtaBase
        // Passing data to Adapter
        myUserViewModel.readAll.observe(viewLifecycleOwner, Observer { DataInputUser->
            myAdapter.updateRecyclerView(DataInputUser)
        })

        // Basically FindViewById
        binding.actionButton.setOnClickListener{
            findNavController().navigate(R.id.action_listFragment_to_addFragment)
        }

        // Return the view that will pop up
        return view
    }

    override fun onDestroy(){
        // To destory binding and prevent memory leak
        listBinding = null
        super.onDestroy()
    }
    
    override fun onDestroyView() {
        super.onDestroyView()
        listBinding = null
    }
}

请帮忙 - 我不知道从哪里开始。

您认为我从数据库获取金额所使用的 SQL 是错误的吗?如何将 DAO 中的此功能实现到我的fragmentActivity 中? 这是代码(您可以在 DAO 文件中看到它):

@Query ("SELECT SUM(amountFood) FROM user_table WHERE typeFood ='Carbs' ")
    fun sumCarbs(): LiveData<Int>

感谢您的时间和帮助。

android sql kotlin android-room
1个回答
0
投票

几个问题:

  1. 您目前得到的结果是什么?
  2. “碳水化合物”区分大小写。您可以显示如何插入到表中。
© www.soinside.com 2019 - 2024. All rights reserved.