vendredi 27 octobre 2017

checking a checkbox in recyclerview making other random checkboxes checked too android

I have one recyclerview adapter which has a checkbox.. when i try to check one box recyclerview makes random checkboxes checked too.. Actually i found the error its because all checkboxes have one state and reuses the view.. and also i have many options posted in stackoverflow but nothing worked for me..

This below am checking in the table if there is any item then that particular item checkbox should be checked..

if(Objects.equals(orderTable.getItemCode(), items.getItemCode()) && Objects.equals(orderTable.getStoreId(), storesPojos.getId()))
                {
                 /*   Log.e(TAG+" Order Table Code", String.valueOf(orderTable.getItemCode()));
                    Log.e(TAG+" ItemsPojo Code", String.valueOf(items.getItemCode()));

                    Log.e(TAG +" Order Table ID",orderTable.getStoreId());
                    Log.e(TAG+" store ID",orderTable.getStoreId());*/

                    Log.e("Position of Adapter///", String.valueOf(position));
                    Log.e("SelectedPosition of Adapter///", String.valueOf(selectedPosition));
                    if(selectedPosition == position){
                        holder.checkBox.setChecked(true);
                    }
                    else{
                        holder.checkBox.setChecked(false);
                    }
                    checked++;
                    noOfItems.setText(String.valueOf(checked));
                }

This below code is my checkbox.setOnCheckedChangeListener()

holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                ItemsPojo itemsPojo = itemList.get(position);

                if(selectedPosition == position)
                {
                    checked++;
                    noOfItems.setText(String.valueOf(checked));
         dbHelper.insertItemFromOrderTable(itemsPojo.getItemCode());
                }
                else
                {
                    checked--;
                    noOfItems.setText(String.valueOf(checked));

          dbHelper.deleteItemFromOrderTable(itemsPojo.getItemCode());
                }




Aucun commentaire:

Enregistrer un commentaire