问题描述
我有一个问题,是否可以选择使用适配器中的可编辑文本从ROOM中的列计算数据?适配器:
I have a question if there is any option to calculate data from column in ROOM with editable Edit Text in adapter?Adapter:
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中,是我们可以输入区域的位置,程序应显示该区域所需农药的计算剂量列表
EditText is in activity_main.xml as place where we can type our area and program should show a list of calculated dose of pesticide needed to this area
推荐答案
您需要通过查询来做到这一点,并且还需要一个中间模型来做到这一点:
You need to do it with a query and also have an intermediate Model for doing it so:
Room是SQLite的包装器,因此大多数在SQLite上有效的内容都对ROOM有效
Room is a SQLite wrapper, so mostly all valid on SQLite is valid for 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>
因此您可以看到某物
,这是因为我们要在 DosedOpryski
So as you can see there is something as something
that is because we are going to reflect all those values in the DosedOpryski
data class DosedOpryski(
val name: String, val what: String, val when: String, val multiplication: Int
)
这篇关于如何将ROOM数据库的列与编辑文本相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!