使用Firebase回收器适配器实现共享意向 [英] Iimpalement share intent, using Firebase recycler adapter

查看:79
本文介绍了使用Firebase回收器适配器实现共享意向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FirebaseRecylcerAdapter,尝试使用共享意图将RecylcerView的图像或数据共享给其他应用程序

I am using FirebaseRecylcerAdapter, trying to share the image or data of my RecylcerView to other apps using share intent

下面是我的适配器,我要在该适配器上单击按钮:

below is my adapter where I am trying to get the button onclick:

  private fun ShowData() {
   var tip =  mfirebasedatabase!!.getReference("Model").orderByChild("a")
    options = FirebaseRecyclerOptions.Builder<Model>().setQuery(tip, Model::class.java).build()
    mfirebaseRecyclerAdapter = object : FirebaseRecyclerAdapter<Model, ViewHolder>(options!!) {
        override fun onBindViewHolder(viewHolder: ViewHolder, i: Int, model: Model) {
            viewHolder.setDetails(applicationContext, model.image, model.title)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val itemview = LayoutInflater.from(parent.context).inflate(R.layout.row, parent, false)
            val viewHolder = ViewHolder(itemview)
            var sh :Button = itemview.findViewById<Button>(R.id.buttontoShare)
            sh.setOnClickListener {
              // how to get the Model.image so that i can implement
                //share intent to share data to other apps?

            }

请帮助我了解如何从那里获取对象

Please help me understand how to get the object from there

推荐答案

onCreateViewHolder()方法中不能使用在onBindViewHolder()方法中作为参数获取的model对象,因为该对象超出了范围.如果需要在sh按钮上设置单击侦听器,请在ViewHolder类而不是在onCreateViewHolder方法中进行设置.这样,您可以访问model对象,因为在使用以下对象时,该对象早已发送到该类中:

The model object that you get as an argument in the onBindViewHolder() method cannot be used in onCreateViewHolder() method because is out of scope. If you need to set on click listener on the sh button, set it in the ViewHolder class rather than in onCreateViewHolder method. In this way, you can access the model object because is already send into that class earlier, when you have used:

viewHolder.setDetails(applicationContext, model.image, model.title)

这样,您可以附加侦听器并将单击的对象发送到下一个活动.但是,解决此问题的更合适方法是使用回调,如本

In that way, you can attach the listener and send the clicked object to the next activity. However, a more appropriate way of solving this would be to use a callback, as explained in this repo.

这篇关于使用Firebase回收器适配器实现共享意向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆