Reputation: 565
I want to send multiple checkbox values to Firebase database. I took three Checkboxes in XML and a Button. I want to store selected checkbox text to Firebase when the Button is clicked and go to another Activity. This is my code, but it isn't working.
mFirstCheckBox = findViewById(R.id.firstCheckBox);
mSecondCheckBox = findViewById(R.id.secondCheckBox);
mThirdCheckBox = findViewById(R.id.thirdCheckBox);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
mAuth = FirebaseAuth.getInstance();
saveButton = findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.firstCheckBox:
if(mFirstCheckBox.isChecked()) {
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = mDatabase.child(user_id);
current_user_db.child("1").setValue("Compiler design");
}
break;
case R.id.secondCheckBox:
if(mSecondCheckBox.isChecked()) {
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = mDatabase.child(user_id);
current_user_db.child("2").setValue("Operating Systems");
}
break;
case R.id.thirdCheckBox:
if(mThirdCheckBox.isChecked()) {
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = mDatabase.child(user_id);
current_user_db.child("3").setValue("Software Engineering");
}
break;
}
Intent interestIntent = new Intent(HomeActivity.this, InterestsActivity.class);
interestIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(interestIntent);
}
});
This is what I want to achieve in Firebase database, but not getting. In between 'Users' and 'Values(1,2,3)' there must be user_Id. it is missing in the diagram because I have entered them in Firebase
Please Guide me, where I am getting wrong. Tell me any other alternative to get this done, if I am completely wrong.
Thanks in advance.
Upvotes: 0
Views: 6404
Reputation: 138969
First, when you are using this view.getId()
, you are getting the id of the button that was clicked and not the id of your checkbox that was checked. Second, there is no need to use the following line of code in each case of your switch statement:
String user_id = mAuth.getCurrentUser().getUid();
It is enough to be used only once. In fact, there is no need to use a switch statement at all. Your code should look like this:
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = mDatabase.child(user_id);
if(mFirstCheckBox.isChecked()) {
current_user_db.child("1").setValue("Compiler design");
}
if(mSecondCheckBox.isChecked()) {
current_user_db.child("2").setValue("Operating Systems");
}
if(mThirdCheckBox.isChecked()) {
current_user_db.child("3").setValue("Software Engineering");
}
Intent interestIntent = new Intent(HomeActivity.this, InterestsActivity.class);
interestIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(interestIntent);
}
});
Upvotes: 2
Reputation: 758
CheckBoxes only have two states - checked and unchecked, i.e. true and false.
Therefore, you're correct in saying a String isn't a suitable datatype
for holding information given by a CheckBox.
What you want to use is a boolean.
Treat CheckBoxes as having boolean values (the isChecked()
method each CheckBox has will probably come in handy),
and save them .
Upvotes: 0