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.
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