jeudi 30 janvier 2020

How to make select all checkbox and delete selected item in kotlin?

Outside the recycler view, there are Select All and Delete Select. I'm already working on creating a Select Delete button and a Select All button. However, if I click one checkbox before clicking Select All, and then click Select All, the selected checkbox is unchecked. My delete function also works strangely. I must delete the checked items, but other items are deleted. How can I solve this problem? How do I create a loop that checks a checkbox? Please look at my code and help me.

CartViewActivity

class CartViewActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefreshListener {




    internal lateinit var mAdapter: CartItemRecyclerAdapter

    @RequiresApi(Build.VERSION_CODES.N)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_cart_view)

        val page = 0
        val token = SharedPreference.getTokenInfo(this)
        SharedPreference.removeAll(this, "key")
        Client.retrofitService.getCart(token,page).enqueue(object :Callback<CartResponse> {
            override fun onResponse(call: Call<CartResponse>, response: Response<CartResponse>) {
                if (response?.isSuccessful == true) {
                    swipeRefreshLo.setOnRefreshListener(this@CartViewActivity)
                    showdata(response.body()?.docs!!)

                }else if(response?.isSuccessful==false) {
                    val er = Gson().fromJson(response.errorBody()?.charStream(), ErrorResponse::class.java)
                    if (er.code==60202) {

                    }
                }
            }
            override fun onFailure(call: Call<CartResponse>, t: Throwable) {

            }

        })

        selectAll.setOnClickListener{

            if (selectAll.isChecked) {
                selectAll.buttonDrawable = it.context.getDrawable(R.drawable.check_box_active_cs)
                mAdapter.toggleItems()
                val layout = findViewById(R.id.layoutOrder) as LinearLayout
                layout.visibility= View.VISIBLE
            }else {
                selectAll.buttonDrawable = it.context.getDrawable(R.drawable.check_box_no)
                mAdapter.toggleItems()
                val layout = findViewById(R.id.layoutOrder) as LinearLayout
                layout.visibility = View.GONE
            }

        }

        selectDelete.setOnClickListener {
               val items = SharedPreference.readSelect(this, "key")
                val param = select((items))
                Client.retrofitService.removeCart(token, param).enqueue(object : Callback<deleteResponse>{
                    override fun onResponse(call: Call<deleteResponse>, response: Response<deleteResponse>
                    ) {

                            SharedPreference.removeAll(applicationContext, "key")
mAdapter.removeItems()

                    }
                    override fun onFailure(call: Call<deleteResponse>, t: Throwable) {

                    }

                })
            }
        }

    private fun showdata(results: ArrayList<cartDocs>) {
        recycler_view.apply {
            mAdapter=CartItemRecyclerAdapter(context,context as Activity, results)
            recycler_view.adapter=mAdapter
            recycler_view.layoutManager=LinearLayoutManager(context)

        }
    }

    override fun onRefresh() {
        swipeRefreshLo.isRefreshing = false
    }
  }

CartItemRecyclerAdapter

class CartItemRecyclerAdapter(val context: Context, private var activity : Activity, private val dataList: ArrayList<cartDocs>) : RecyclerView.Adapter<CartItemRecyclerAdapter.Holder>() {


    companion object {
        var checkedItems = ArrayList<cartDocs>()
    }


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

    override fun onBindViewHolder(holder: Holder, position: Int) {
        holder.bind(dataList.get(position), context)
        Glide.with(context).load(dataList.get(position).mainImage).into(holder.imageView)

    }

    override fun getItemCount(): Int = dataList.size


    @RequiresApi(Build.VERSION_CODES.N)
    fun removeItems(                                                                                                                                                                                                                                                                                                                                                                                                                       ) {
        dataList.removeIf { it.isSelected }
        notifyDataSetChanged()
    }


    fun toggleItems() {
        for (item: cartDocs in dataList) {
            var state = item.isSelected
            item.isSelected = !state

        }
        notifyDataSetChanged()
    }


    inner class Holder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {


        var titleText = itemView?.findViewById(R.id.titleText) as TextView
        var temNumerTextt = itemView?.findViewById(R.id.textViewItemNumer) as TextView
        var priceText = itemView?.findViewById(R.id.priceText) as TextView
        var pointValueText = itemView?.findViewById(R.id.pointValueText) as TextView
        var imageView = itemView?.findViewById(R.id.imageView) as ImageView
        var checkBox = itemView?.findViewById(R.id.checkBox) as CheckBox
        var deleteBox = itemView?.findViewById(R.id.delete) as Button
        var order = itemView?.findViewById(R.id.btnOrder) as Button


        val token = SharedPreference.getTokenInfo(context)



        fun bind(data: cartDocs, context: Context) {
            val dec = DecimalFormat("##,000")
            priceText.text = dec.format(data.price).toString() 
            titleText.text = data.title
            temNumerTextt.text = data.amount.toString()
            pointValueText.text = dec.format(data.price).toString()


            val value3 = dataList.size.toString()
            if (value3 != null) {
                SharedPreference.setThirdText(context, value3)
            }
            if (data.isSelected) {
                checkBox.buttonDrawable =
                    checkBox.context.getDrawable(R.drawable.check_box_active_cs)
                val layout = activity.findViewById(R.id.layoutOrder) as LinearLayout
                layout.visibility = View.VISIBLE
            } else {
                checkBox.buttonDrawable = checkBox.context.getDrawable(R.drawable.check_box_no)
                val layout = activity.findViewById(R.id.layoutOrder) as LinearLayout
                layout.visibility = View.GONE
            }


            order.setOnClickListener {
                val i = Intent(it.context, PaymentActivity::class.java)
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                it.context.startActivity(i)

            }


            deleteBox.setOnClickListener {
                val item = data.id
                Client.retrofitService.deleteCart(token, item)
                    .enqueue(object : retrofit2.Callback<deleteResponse> {
                        override fun onResponse(
                            call: Call<deleteResponse>, response: Response<deleteResponse>
                        ) {
                            dataList.removeAt(adapterPosition)
                            notifyItemRemoved(adapterPosition)
                            notifyItemRangeChanged(adapterPosition, dataList.size)
                            val layout = activity.findViewById(R.id.layoutOrder) as LinearLayout
                            layout.visibility = View.GONE

                        }

                        override fun onFailure(call: Call<deleteResponse>, t: Throwable) {

                        }
                    })
            }


            checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
                data.isSelected = !data.isSelected
                if (isChecked) {
//                   
                    checkBox.buttonDrawable = context.getDrawable(R.drawable.check_box_active_cs)
                    val item = data.id
                    val listSelected: ArrayList<String> =
                        SharedPreference.readSelect(context, "key")
                    listSelected.add(item!!)
                    SharedPreference.saveSelect(context, listSelected, "key")
                    val layout = activity.findViewById(R.id.layoutOrder) as LinearLayout
                    layout.visibility = View.VISIBLE
                } else {
                    SharedPreference.cartRemove(context, "key")
                    checkBox.buttonDrawable = context.getDrawable(R.drawable.check_box_no)
                    val layout = activity.findViewById(R.id.layoutOrder) as LinearLayout
                    layout.visibility = View.GONE
                }

                checkedItems.clear()
                for (cartDocs in dataList) {
                    if (cartDocs.isSelected) {
                        checkedItems.add(cartDocs)
                    }
                }

                Log.d("cart", "checked items : " + checkedItems.count())
                if (checkedItems.count()!==0) {
                    val layout = activity.findViewById(R.id.layoutOrder) as LinearLayout
                    layout.visibility = View.VISIBLE
                }else if (checkedItems.count()==0) {
                    val layout = activity.findViewById(R.id.layoutOrder) as LinearLayout
                    layout.visibility = View.GONE
                }


            }
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire