通过kotlin ..通过firebase从数据库读取数据到RecyclerView的麻烦
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过kotlin ..通过firebase从数据库读取数据到RecyclerView的麻烦相关的知识,希望对你有一定的参考价值。
我很难通过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"
答案
可能遗漏:
myadapter.notifyDataSetChanged()
以上是关于通过kotlin ..通过firebase从数据库读取数据到RecyclerView的麻烦的主要内容,如果未能解决你的问题,请参考以下文章
从 Firebase 实时数据库 Kotlin 中检索有序数据
当有唯一键 Kotlin 时如何从 Firebase 检索孩子
从 Firebase android kotlin 获取数据时如何显示加载的项目