lundi 29 juillet 2019

How to set Checkbox OnCheckedChangeListener with firebaseRecyclerAdapter

I am using firebase recycler adapter to download checkbox items. However to retrieve them I would like to set a listner. OnclickedListener doesn't work. And OnCheckedListener is not accessible with the "viewHolder.itemview".

@Override
        public CheckBoxHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            CheckBoxHolder viewHolder =  super.onCreateViewHolder(parent, viewType);

                     //this variable returns null
            viewHolder.post_business_type.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Log.d("myTagType", "type clicked ");
                    String parentID = firebaseRecyclerAdapter.getRef(editType.getChildLayoutPosition(buttonView)).getKey();
                    Log.d("myTagTypes", parentID);
                    if (isChecked) {
                        types.add(parentID);
                    } else {
                        for (int i = 0; i < types.size(); i++) {
                            if (types.get(i).equals(parentID)) {
                                types.remove(i);
                            }
                        }
                    }
                }
            });
                    //this does not work with checkbox
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d("myTagType", "type clicked ");
                    String parentID = firebaseRecyclerAdapter.getRef(editType.getChildLayoutPosition(v)).getKey();
                    Log.d("myTagTypes", parentID);
                    if (v.isPressed()) {
                        types.add(parentID);
                    } else {
                        for (int i = 0; i < types.size(); i++) {
                            if (types.get(i).equals(parentID)) {
                                types.remove(i);
                            }
                        }
                    }

                }
            });
            return viewHolder;
        }
    };
    editType.setAdapter(firebaseRecyclerAdapter);
}

public static class CheckBoxHolder extends RecyclerView.ViewHolder {
    View view;
    AppCompatCheckBox post_business_type;
    public  CheckBoxHolder(View itemView) {
        super(itemView);
        view = itemView;
    }
    public void setType(String businessType) {
        //Log.d("myTagType", "fetching types3 ");
        post_business_type = view.findViewById(R.id.reg_checks);
        //Log.d("myTagType", "type is: " + businessType);
        post_business_type.setText(businessType);
    }
}

So I have used this same method with other items such as pictures and textviews etc. But this is the first time it is not working so I have assumed that the OnClickedListener doesn't work and need to find a way to access OnCheckedListener




Aucun commentaire:

Enregistrer un commentaire