Android RecyclerView With Kotlin - Kotlin Android example

Recyclerview with kotlin - Kotlin Android example

DTO

class CartListItemResponse(var title: String, var price: String, var quantity: String, var description: String, var image: String?) {
    override fun toString(): String {
        return "WishListItem{" +
                "title='" + title + '\''.toString() +
                ", price='" + price + '\''.toString() +
                ", quantity='" + quantity + '\''.toString() +
                ", description='" + description + '\''.toString() +
                ", image='" + image + '\''.toString() +
                '}'.toString()
    }
}

CartListActivity.kt

class CartListActivity : AppCompatActivity() {

    private var cartListItemArray: ArrayList<CartListItemResponse> = arrayListOf()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cart_list)

        cartListItemArray.add(CartListItemResponse("Red Rum dress","$399","Qty : 1","Small , red",null))
        cartListItemArray.add(CartListItemResponse("Red Rum dress","$399","Qty : 1","Small , red",null))
        cartListItemArray.add(CartListItemResponse("Red Rum dress","$399","Qty : 1","Small , red",null))

        rvCartList.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)
        rvCartList.adapter = CartListItemAdapter(this,cartListItemArray);
    }
}
CartListItemAdapter.kt
class CartListItemAdapter(mContext: Context, private val cartListItems: ArrayList<CartListItemResponse>) : RecyclerView.Adapter<CartListItemAdapter.ViewHolder>() {
    private var imageLoader:ImageLoader
    private var displayImageOptions:DisplayImageOptions = Utility.getRadiusCornerDisplayOptions()
    init {
        val imageLoaderConfiguration:ImageLoaderConfiguration = ImageLoaderConfiguration.createDefault(mContext)
        imageLoader = ImageLoader.getInstance()
        imageLoader.init(imageLoaderConfiguration)
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var title: TextView = view.txtProductName
        var desc: TextView = view.txtProductBrand
        var image: ImageView = view.imgProduct
        var quantity: TextView = view.productQty
        var price:TextView = view.productPrice
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val itemView = LayoutInflater.from(parent.context)
                .inflate(R.layout.adapter_cart_list, parent, false)

        return ViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val wishItemList = cartListItems[position]
        holder.title.text = wishItemList.title        holder.desc.text = wishItemList.description        holder.quantity.text=wishItemList.quantity        holder.price.text=wishItemList.price        val uriString:String = "drawable://" + R.drawable.ic_launcher_background;
        imageLoader.displayImage(uriString,holder.image,displayImageOptions)
    }

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

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment

Popular posts from this blog