我无法通过Firebase从数据库读取数据。 该代码应该从数据库读取数据到RecyclerView ..但无法正常工作。
该应用程序正常运行,没有错误或错误,如果在recyclerView中写入了一些数据,则可以毫无问题地进入数据库,但是recyclerView中没有显示数据。
............
MainActivity代码>>>
class MainVote : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_vote)
RecyclerView.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
val votes = ArrayList<VotePublic>()
// Here start reading data code from database in firebase
val db= FirebaseDatabase.getInstance().getReference()
db.child("votedata").addValueEventListener(object : ValueEventListener{
override fun onDataChange(p0: DataSnapshot?) {
val values = p0!!.value as HashMap<String,Any>
for (k in values.keys){
val data = values[k] as HashMap<String,Any>
votes.add(
VotePublic(
data["quesVote"] as String ,
data["vote11"] as String ,
data["vote22"] as String ,
data["vote33"] as String ,
data["vote44"] as String
))
}
val myadaper = CastumAdapter(votes,this)
RecyclerView.adapter = myadaper
}
override fun onCancelled(p0: DatabaseError?) {
}
})
CustomAdapter类。>>
class CastumAdapter(val votelist: ArrayList<VotePublic>, val context: ValueEventListener) : RecyclerView.Adapter<CastumAdapter.MyViewHolder> () {
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHolder {
val view = LayoutInflater.from(p0.context).inflate(R.layout.raw_re2,p0,false)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
return votelist.size
}
override fun onBindViewHolder(holder: MyViewHolder, p1: Int) {
val votepublic : VotePublic = votelist[p1]
holder.setData(votepublic, p1)
}
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
// some code here..
.
.
.
.
fun setData(datavote: VotePublic?, pos: Int) {
this.currentHobby = datavote
this.currentPosition = pos
}
}
}
这是数据库结构的JSON示例:
{
"project_info": {
"project_number": "836053259827",
"firebase_url": "https://vote-app-****",
"project_id": "vote-app-****",
"storage_bucket": "vote-app-****.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:836053259827:android:****",
"android_client_info": {
"package_name": "com.example.dell.vote1"
}
},
"oauth_client": [
{
"client_id": "****-hlqj7at8n5tvhmllgr82p7k9ssvl3f8l.apps.googleusercontent.com",
"client_type": 1,
"android_info": {
"package_name": "com.example.dell.vote1",
"certificate_hash": "****"
}
},
{
"client_id": "****-glt9h6b98k7bc2m1390rbogn7tioo70a.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "****_-9t6BVPeE4g8eQ"
}
],
"services": {
"analytics_service": {
"status": 1
},
"appinvite_service": {
"status": 2,
"other_platform_oauth_client": [
{
"client_id": "****-glt9h6b98k7bc2m1390rbogn7tioo70a.apps.googleusercontent.com",
"client_type": 3
}
]
},
"ads_service": {
"status": 2
}
}
}
],
"configuration_version": "1"
}
答案 0 :(得分:0)
可能丢失:
myadapter.notifyDataSetChanged()