微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Kotlin中获取列表中的所有项目

如何解决如何在Kotlin中获取列表中的所有项目

如何使用实时数据列表比较android kotlin中的另一个列表。我有一个回收站视图,在该视图中,我已经将livedata传递给了构造函数,并已经使用diffutil类填充了另一组数据。我想在两个列表之间进行比较,如何从列表中获取所有项目并初始化为变量并比较它的两个ID。 在我的代码下面:

RecyclerView:

        class CategoryItemsAdapter(private var listener: (CategoryItems,Int) -> Unit,val cartItem: LiveData<List<CategoryItems>>) : RecyclerView.Adapter<CategoryItemsAdapter.MainViewHolder>(){

         private val differCallback = object : DiffUtil.ItemCallback<CategoryItems>(){

    override fun areItemsTheSame(oldItem: CategoryItems,newItem: CategoryItems): Boolean {
        return oldItem.item_id == newItem.item_id
    }

    override fun getChangePayload(oldItem: CategoryItems,newItem: CategoryItems): Any? {
        return super.getChangePayload(oldItem,newItem)
    }

    override fun areContentsTheSame(oldItem: CategoryItems,newItem: CategoryItems): Boolean {
        return oldItem == newItem
    }
}

val differ = AsyncListDiffer(this,differCallback)

override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): MainViewHolder {
    return MainViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.categories_item,parent,false))
}

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

override fun onBindViewHolder(holder: MainViewHolder,position: Int) {
    val categoryItems = differ.currentList[position]


    val cartItems = arraylistof<CategoryItems>()//trying to initialize here
    cartItems.addAll(cartItems)

    holder.itemView.apply {
        Glide.with(this).load(categoryItems.item_url).into(itemImageView)
        item_company.text = categoryItems.item_brand
        item_name.text = categoryItems.item_name
        item_price.text = categoryItems.item_price.toString()+"Rs"
        var quantityValue = 0




        Timber.i("CategorItem: ${categoryItems.item_id} ")
        Timber.i("CartItem: ${cartItems.item_id}")
       
        if (cartItems.item_id == categoryItems.item_id){
            Timber.i("equals something: ${cartItems.item_quantity} ")
            quantityValue = cartItems.item_quantity
            quantity.text = quantityValue.toString()
        }

基本上我想获取所有商品ID并与另一个列表{position}

比较
        if (cartItems.item_id == categoryItems.item_id){
            Timber.i("equals something: ${cartItems.item_quantity} ")
            quantityValue = cartItems.item_quantity
            quantity.text = quantityValue.toString()
        }

kotlin的新手,有人可以帮助我吗!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。