首页 > 解决方案 > 通过 kotlin .. 通过 firebase 从数据库读取数据到 RecyclerView 时遇到问题

问题描述

我无法通过 firebase 从数据库中读取数据。代码应该从数据库读取数据到 RecyclerView..但没有工作..

该应用程序可以正常工作,没有错误或错误,如果在 recyclerView 中写入一些数据将毫无问题地进入数据库,但 recyclerView 中没有数据显示..

…………

主活动代码>>>

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?) {

        }
    })

自定义适配器类.. >>

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"
}

标签: androiddatabasefirebasekotlinandroid-recyclerview

解决方案


可能缺少:

myadapter.notifyDataSetChanged()

推荐阅读