skip to Main Content

i parse json data from https://dummyjson.com/products into recycleview . it show product list . then i set onclcik listener on item and parse image and description, price into new activity . The price and description is parse into new activity bt image is not showing

MyAdapter.kt

package com.fiham.retroapi

import android.app.Activity
import android.content.Intent

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Picasso

class MyAdapter (val context: Activity , val productArrayList : List<Product>) :
RecyclerView.Adapter<MyAdapter.MyViewHolder> (){



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

       val itemview = LayoutInflater.from(context).inflate(R.layout.single_item, parent, false)
        return  MyViewHolder(itemview )

    }

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

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

        val currentItem = productArrayList[position]
        holder.title.text = currentItem.title
        holder.rating.text = currentItem.rating.toString()
        //imageview
        Picasso.get().load(currentItem.thumbnail).into(holder.image)

        holder.constraint_row.setOnClickListener {
            val cont = holder.constraint_row.context

            val intent = Intent(it.context, ProductDetails::class.java)

            intent.putExtra("image" , currentItem.thumbnail)
            intent.putExtra("price" , currentItem.price)
            intent.putExtra("description" , currentItem.description)

            it.context.startActivity(intent)

        }




    }

    class MyViewHolder (itemview : View) : RecyclerView.ViewHolder (itemview) {

        var title : TextView
        var rating : TextView
        var image : ImageView
        var constraint_row : ConstraintLayout = itemview.findViewById(R.id.constraint_row)

        init {
            title = itemview.findViewById(R.id.tv2)
            image = itemview.findViewById(R.id.iv1)
            rating = itemview.findViewById(R.id.rating)


        }



    }

}

Prodcut.kt

package com.fiham.retroapi

data class Product(
    val brand: String,
    val category: String,
    val description: String,
    val discountPercentage: Double,
    val id: Int,
    val images: List<String>,
    val price: Int,
    val rating: Double,
    val stock: Int,
    val thumbnail: String,
    val title: String
)

Productdetails.kt

package com.fiham.retroapi

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView


class ProductDetails : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_product_details)

        val thumbnail = findViewById<ImageView>(R.id.thumbnail)
        val instock = findViewById<TextView>(R.id.instock)
        val desc = findViewById<TextView>(R.id.desc)


        val intent = intent

        val image  = intent?.getIntExtra("image" , 0 )
        val price  = intent?.getIntExtra("price" ,0)
        val description  = intent?.getStringExtra("description" )


        if (image != null) {
            thumbnail.setImageResource(image).toString()
        }


        instock.text = price.toString()
        desc.text = description
    }
}

i want to parse image into new activity which comes from my adapter

2

Answers


  1. When we pass data of a type, we must receive this data of the same type

    The problem is that you pass data from type and receive this data from another type

    MyAdapter.kt

    holder.constraint_row.setOnClickListener {
                val cont = holder.constraint_row.context
    
                val intent = Intent(it.context, ProductDetails::class.java)
                
                //currentItem.thumbnail is string in Prodcut.kt 
                intent.putExtra("image" , currentItem.thumbnail)
                intent.putExtra("price" , currentItem.price)
                intent.putExtra("description" , currentItem.description)
    
                it.context.startActivity(intent)
    
            }
    

    Productdetails.kt

    class ProductDetails : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_product_details)
    
            val thumbnail = findViewById<ImageView>(R.id.thumbnail)
            val instock = findViewById<TextView>(R.id.instock)
            val desc = findViewById<TextView>(R.id.desc)
    
    
            val intent = intent
       
            //We receive data of the same type 
            val image  = intent?.getStringExtra("image")
            val price  = intent?.getIntExtra("price" ,0)
            val description  = intent?.getStringExtra("description" )
    
    
            if (image != null) {
                //The image is not in the drawble folder until we use this
               // thumbnail.setImageResource(image).toString()
               //We are receiving a link that we want to display this image 
               //from the api
              //imageview
              Picasso.get().load(image).into(thumbnail)
    
            }
    
    
            instock.text = price.toString()
            desc.text = description
        }
    }
    
    Login or Signup to reply.
  2. you have to use this

    Glide.with(context).load(image).placeholder(R.drawable.no_image).into(thumbnail)
    

    Noted R.drawable.no_image is the default picture when not get any data then show this picture default

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search