您好这是一个示例应用程序,用于保存数据并从FireBase中读取数据并将其与listview一起放入适配器我没有任何保存数据的问题我的问题是在保存之后阅读它(onDataChange)我找不到哪里有错误!
class ShowData : AppCompatActivity() {
lateinit var Ref:DatabaseReference
lateinit var MyList:MutableList<ListData>
lateinit var adapter:ListDataAdabter
lateinit var myview:ListView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_show_data)
MyList= mutableListOf()
myview=findViewById(R.id.listview)
LoadData()
adapter= ListDataAdabter(MyList,this)
myview.adapter=adapter
}
fun LoadData(){
Ref=FirebaseDatabase.getInstance().getReference("MyTexts")
Ref.addValueEventListener(object :ValueEventListener{
override fun onCancelled(p0: DatabaseError?) {
Toast.makeText(this@ShowData,"Error witj your Data",Toast.LENGTH_LONG).show()
}
override fun onDataChange(p0: DataSnapshot?) {
if (p0!!.exists()){
Toast.makeText(this@ShowData,"The Data is Here",Toast.LENGTH_LONG).show()
MyList.clear()
for (e in p0.children){
val values = e.getValue(ListData::class.java)
MyList.add(values!!)
}
}
}
})
}
}
列表适配器
class ListDataAdabter(List:MutableList<ListData>,mCtx:Context): BaseAdapter() {
var ListLocal = List
var context = mCtx
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
val item = ListLocal[p0]
val inflater = LayoutInflater.from(context)
val view = inflater.inflate(R.layout.list_type,null)
view.TV_Fname.text=item.Fname
view.TV_Lname.text=item.Lname
view.TV_Age.text=item.Age
return view
}
override fun getItem(p0: Int): Any {
return ListLocal[p0]
}
override fun getItemId(p0: Int): Long {
return p0.toLong()
}
override fun getCount(): Int {
return ListLocal.size
}
}