У меня проблемы с чтением данных из базы данных с помощью 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 .. >>
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"
}