繁体   English   中英

如何将 ROOM 数据库的列与编辑文本相乘

[英]How to multiplicate ROOM database's columns with edit text

我有一个问题,是否可以使用适配器中的可编辑编辑文本来计算 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 中,我们可以在其中键入我们的区域,程序应该显示该区域所需农药的计算剂量列表

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

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
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM