Home > Net >  How to pass Firebase Realtime database images from reyclerview to image view in another activity. KO
How to pass Firebase Realtime database images from reyclerview to image view in another activity. KO

Time:06-18

I have Firebase Realtime Database images displayed in a reyclerview. When users click that image, I want it to send the image to another activity's IMAGEVIEW and open that activity at the same time.

These are pictures of an example I saw on Youtube

Here they have a recyclerview that displays Firebase images

enter image description here

As you can see in the second picture. It sent the data from the recyclerview to the imageview in the activity and opened that activity.

My adapter class

class AbstractAdapter(private val mContext: Context, private val abstractList: ArrayList<Abstract>) :
RecyclerView.Adapter<AbstractAdapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.abstract_image_view, parent, false)
    return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    holder.imageView.setOnClickListener {
        val intent = Intent(mContext, PreviewActivity::class.java)
        intent.putExtra("abstract", abstractList[position].abstract.toString())
        mContext.startActivity(intent)
    }

    holder.download_btn.setOnClickListener {  }

    Glide.with(mContext)
        .load(abstractList[position].abstract)
        .into(holder.imageView)
}

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

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val imageView: ImageView = itemView.findViewById(R.id.abstractImageView)
    val download_btn: Button = itemView.findViewById(R.id.abstractDownloadBtn)

}

companion object {
    private const val Tag = "RecyclerView"
}

Activity to receive image

class PreviewActivity : AppCompatActivity() {

private lateinit var previewImage: ImageView

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_preview)

   previewImage = findViewById(R.id.preview_image)

    val previewImage: ImageView = findViewById(R.id.preview_image)
    val bundle :Bundle? = intent.extras
    val preview = bundle!!.getString("abstract")

}

Data model

    class Abstract(var abstract: String? = null) {
}

I've tried running this code, but it doesn't show my image in the next activity. The Logcat doesn't give me any error, because according to it my code is running perfectly except that it's not showing the image in the next activity. I must be missing something, but I don't know what it is.

CodePudding user response:

You have to set the image url to ImageView using Glide in second activity like this:

class PreviewActivity : AppCompatActivity() {

private lateinit var previewImage: ImageView

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_preview)

    previewImage = findViewById(R.id.preview_image)

    val previewImage: ImageView = findViewById(R.id.preview_image)
    val bundle :Bundle? = intent.extras
    val preview = bundle!!.getString("abstract")

    Glide.with(this)
       .load(preview)
       .into(previewImage)
}
  • Related