【问题标题】:How to multiplicate ROOM database's columns with edit text如何将 ROOM 数据库的列与编辑文本相乘
【发布时间】:2021-04-22 10:49:14
【问题描述】:

我有一个问题,是否可以使用适配器中的可编辑编辑文本从 ROOM 中的列计算数据? 适配器:

class AdapterList (context: Context, val resource: Int, val objects: List<Opryski>) :
    ArrayAdapter<Opryski>(context, resource, objects){
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
    val inflater = LayoutInflater.from(context)
    val customView = inflater.inflate(resource, parent, false)
    val area= customView.findViewById<EditText>(R.id.editTextSize)
    val dose= customView.findViewById<TextView>(R.id.id_dose)
    val name= customView.findViewById<TextView>(R.id.id_nazme)
    val what= customView.findViewById<TextView>(R.id.id_what)
    val when= customView.findViewById<TextView>(R.id.id_when)
    val profilaktyka = customView.findViewById<TextView>(R.id.id_profilaktyka)

    val item = objects.getOrNull(position)
    if(item!=null)
    {
        name.text = item.name
        dose.text = item.dose.toString() * area //<--- I want to multiplicate this//
        what.text = item.what
        when.text = item.when
        profilaktyka.text = item.profilaktyka
    }
return customView
}

}

EditText 在 activity_main.xml 中,我们可以在其中输入我们的区域,程序应该显示该区域所需农药的计算剂量列表

【问题讨论】:

    标签: android kotlin android-edittext android-room


    【解决方案1】:

    您需要通过查询来执行此操作,并且还需要一个中间模型来执行此操作:

    Room 是一个 SQLite 包装器,所以大部分在 SQLite 上有效的都对 ROOM 有效

    //I'm using a raw string here, triple quotes so I can write without minding the line breaks
    @Query("""SELECT 
    name as name,
    what as what,
    when as when,
    (dose * :area) as multiplication
    FROM Opryski
    """)
    fun observeOpryskiesWithCalculatedDose(area: Int): LiveData<DosedOpryski>
    

    如您所见,something as something 是因为我们将在 DosedOpryski 中反映所有这些值

    data class DosedOpryski(
        val name: String, val what: String, val when: String, val multiplication: Int
    )
    

    【讨论】:

      猜你喜欢
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多