来自firebase的recyclerview onClick getkey

时间:2018-01-15 15:36:21

标签: android firebase firebase-realtime-database kotlin

如何在RecyclerView中获取kotlin密钥以将其发送到另一个活动? 我尝试使用getKey()position,但它不起作用。

这是我的代码:

class RecyclerAdapter (val userlist:ArrayList<RecyClass>,ccc: Context): RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
    var mcontext =ccc

    val  Postdata = java.util.ArrayList<RecyClass>()


    lateinit var  ref:DatabaseReference

    override fun getItemCount(): Int {
        return  userlist.size
    }


    override fun onBindViewHolder( holder: ViewHolder?, position: Int) {
        val user: RecyClass = userlist[position]
        ref = FirebaseDatabase.getInstance().getReference(user.toString())

        holder?.txtviewdesc?.text = user.desc
        Picasso.with(mcontext).load(user.image).into(holder?.imageviewx)
        holder?.setOncustomcilcklistner(object :Custumclicklistner33{
            override fun oncustomOnClickListner(view: View, pos: Int) {
                   }

        })
    }




    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
      this.Postdata

         this.mcon
        val v = LayoutInflater.from(parent?.context).inflate(R.layout.layoutrcview,parent,false)
        return ViewHolder(v,mcon,userlist)
    }

    class ViewHolder(itemview: View,ccc2:Context,userlist: ArrayList<RecyClass>): RecyclerView.ViewHolder(itemview), View.OnClickListener{
        var mCustIemClick5: Custumclicklistner33?=null
        var txtviewdesc: TextView
        var mcon =ccc2
        var userdata =userlist
        var imageviewx: ImageView

        init {
             this.userdata
            this.mcon
            txtviewdesc = itemview.findViewById<TextView>(R.id.editTextdesc)
            imageviewx = itemview.findViewById<ImageView>(R.id.imgvw)

            itemview.setOnClickListener(this)
        }


        fun setOncustomcilcklistner(customclick5: Custumclicklistner33){
            this.mCustIemClick5=customclick5
        }

        override fun onClick(view: View?) {
            this.mCustIemClick5!!.oncustomOnClickListner(view!!,adapterPosition)
            val pos = adapterPosition
           val ref =FirebaseDatabase.getInstance().getReference(pos.toString()).key.toString()
            Toast.makeText(mcon,ref,Toast.LENGTH_LONG).show()
            var postDetail = this.userdata[pos]
             val kkk = Intent(this.mcon,ProfileActivity::class.java)
            kkk.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
           this.mcon.startActivity(kkk)
        }
    }
}

fun onclick转到下一个活动,但需要帖子key。怎么弄? getrefpostiton无效fun onclick,但适用于bindviewholder

1 个答案:

答案 0 :(得分:0)

您需要使用PostData [position]来获取当前位置的对象,然后从该对象获取您可以引用的键/字段。