我想做的是比较Firebase数据库中某个字段的textview值,并在它们相等时显示数据。
示例我有一个textview =“ mirid bug”。
这是我的数据库,我希望在其中检索特定数据,因为它的 name 字段等于“ mirid bug”
if(textview == name){ 显示Firebase中具有 name 字段的所有数据 }
这是我的代码:
class classify : AppCompatActivity(){
lateinit var listView: ListView
lateinit var ref: DatabaseReference
lateinit var beetleList:MutableList<Beetle>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_classify)
val capturedImage = intent.extras.getParcelable("captured_image") as Bitmap?
val beetleName=intent.getStringExtra("Beetle")
imageCaptured2.setImageBitmap(capturedImage)
textBeetleName.setText(beetleName)
listView = findViewById(R.id.beetle_list)
beetleList = mutableListOf()
ref = FirebaseDatabase.getInstance().getReference("beetle")
ref.addValueEventListener(object : ValueEventListener{
override fun onCancelled(p0: DatabaseError) {
}
override fun onDataChange(p0: DataSnapshot) {
if(p0!!.exists()){
beetleList.clear()
for (e in p0.children){
val beetle = e.getValue(Beetle::class.java)
beetleList.add(beetle!!)
}
val adapter = BeetleCardAdapter(this@classify,R.layout.beetle_card_item,beetleList)
listView.adapter = adapter
}
}
})
}
}
这是适配器:
class BeetleCardAdapter (val mCtx : Context, val layoutId:Int , val beetleList:List<Beetle>)
:ArrayAdapter<Beetle>(mCtx,layoutId,beetleList){
override fun getView(position: Int, convertView:View?, parent: ViewGroup?): View{
val layoutInflater : LayoutInflater = LayoutInflater.from(mCtx)
val view:View = layoutInflater.inflate(layoutId, null)
val n =view.findViewById<TextView>(R.id.beetle_name)
val name =view.findViewById<TextView>(R.id.person_card_first_name)
val sciName =view.findViewById<TextView>(R.id.person_card_last_name)
val beetleDesc =view.findViewById<TextView>(R.id.person_card_skill)
val beetle = beetleList[position]
n.text = beetle.name
name.text = beetle.Common_Name
sciName.text = beetle.Scientific_Name
beetleDesc.text = beetle.description
return view
}
}