jeudi 23 novembre 2017

Really strange behaviour in android firebaseRecycleAdapter checkbox

I am trying to save the state of a checkbox in a list to Firebase. I have to following layout:

enter image description here

Checking the first box works fine. It saves the state of the checkbox to firebase. However, up on clicking the second checkbox, the first checkbox will mimic the behaviour of the second one. Meaning they get both saved as the same state to firebase. This adds up until all the checkboxes share the same state.

Here is the code where to logic happends:

            protected void populateViewHolder(final ListsViewHolder viewHolder, Lists model, final int position) {

            final String list_id = getRef(viewHolder.getAdapterPosition()).getKey();

            mDoneListRef.child(list_id).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()) {

                        final String title = dataSnapshot.child("title").getValue().toString();
                        viewHolder.setTitle(title);

                        final String detail = dataSnapshot.child("detail").getValue().toString();
                        viewHolder.setDetail(detail);

                        final Boolean ticked = (Boolean) dataSnapshot.child("ticked").getValue();
                        viewHolder.setTicked(ticked);

                        CheckBox listCheckbox = (CheckBox) viewHolder.mListView.findViewById(R.id.list_single_checkbox);

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

                                Log.i("checkboxposition", String.valueOf(viewHolder.getAdapterPosition()));

                                if (isChecked){
                                        mDoneListRef.child(list_id).child("ticked").setValue(Boolean.valueOf(true));
                                    } else {
                                        mDoneListRef.child(list_id).child("ticked").setValue(Boolean.valueOf(false));
                                    }

                            }
                        });


                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }

And here is the setTicked function:

        public void setTicked(Boolean ticked){
        CheckBox checkbox = (CheckBox) mListView.findViewById(R.id.list_single_checkbox);
        if (ticked == true){
            checkbox.setChecked(true);
        } else {
            checkbox.setChecked(false);
        }
    }

I tried to log the behaviour of the checkbox position, which, after a while, resulted in:

enter image description here

I have no clue how this behaviour comes to existance.




Aucun commentaire:

Enregistrer un commentaire