samedi 8 août 2020

Android - How to get all selected checkbox value from recyclerview and store in Firebase?

So, I am trying to get all the values of selected checkboxes in Adapter class and then In my main activity when I click on a button I want checkbox data to get uploaded in Firebase Database. I have tried something but I have no idea how can I store it in Firebase(I know how to store data in Firebase Database but not checkboxes data).

Here is the Code of Adapter Class

public class GroupAdapter extends RecyclerView.Adapter<GroupAdapter.ViewHolder> {
    Context mContext;
    List<Users> mList;
    ArrayList<Users> lstChk= new ArrayList<Users>();

    public GroupAdapter(Context mContext, List<Users> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.display_group_contacts, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Users users = mList.get(position);
        holder.name.setText(users.getUsername());
        holder.number.setText(users.getPhoneNumber());
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked){
                    users.setSelected(true);
                }else {
                    users.setSelected(false);
                }
            }
        });
        holder.checkBox.setSelected(users.getSelected());
        String url = users.getProfilephotoURL();
        if (url.equals(""))
            holder.circleImageView.setImageResource(R.drawable.ic_user);
        else
            Glide.with(mContext).load(url).into(holder.circleImageView);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView name, number;
        CircleImageView circleImageView;
        CheckBox checkBox;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.contactName);
            number = itemView.findViewById(R.id.contactNumber);
            circleImageView = itemView.findViewById(R.id.profilePic);
            checkBox = itemView.findViewById(R.id.checkbox);
        }
    }
}

And in my Main Activity, I have reference to the button and Firebase Database.

So, any idea How can I get selected checkboxes and store the data when I click the button in the Main activity.




Aucun commentaire:

Enregistrer un commentaire