Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] Android – How to get all selected checkbox value from recyclerview and store in Firebase?

Posted on November 14, 2022

Solution 1 :

In your View holder

private Checkbox checkbox;
...
checkbox = itemView.findViewById(R.id.checkbox_id);
...

Now, On upload button click use, holder.checkbox.isChecked();

It returns true if checked and false if not.
Then, you can upload this boolean value to your firebase.

EDIT:
To upload in Firebase,

In your Adapter

private DatabaseReference databaseRef;

In your Adapter’s constructor

databaseRef = FirebaseDatabase.getInstance().getReference("yourRefrence");

Now, finally on upload button Click in OnBindViewHolder

databaseRef.child("something").child(checkbox).setValue(holder.checkbox.isChecked());

Problem :

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.

READ  [ANSWERED] java - I have tried to install Flutter for hours but failed. PLease tell me how to solve this
Powered by Inline Related Posts

Comments

Comment posted by blackapps

Storing “1,1,0,1,0,0,0,1,….” or “true,true,false,true,false,false,false,true,….” is enough. So what is the problem? Just one string defining the state of all checkboxes.

Comment posted by HARSH ASHRA

I am using Recycler view to display data not activity.

Comment posted by Shubham Gupta

The process is same in both cases, still, I edit the answer

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress