Store checkbox values in firebase using android studio

143
November 11, 2018, at 06:00 AM

I am working on recommendation application I using firebase to store information about user.I have used checkboxes for health status information. I want to save all the checkbox values I selected.but in my code if I check more then one checkbox it always save the last checkbox.

This is my code How can I fix it to store all checked values?

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_signup);
   Toolbar toolbar =findViewById(R.id.toolbarotherpages);
    setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

databaseUser = FirebaseDatabase.getInstance("https://bonappetit-808c5.firebaseio.com").getReference("users");
users = new ArrayList<>();
addusername= findViewById(R.id.editTextname);
addphone=findViewById(R.id.editTextphone2);
mFirstCheckBox=findViewById(R.id.cbox1);
mSecondCheckBox=findViewById(R.id.cbox2);
mThirdCheckBox=findViewById(R.id.cbox3);
addhealthstatus=findViewById(R.id.editTexthealthstatus);

btnsignup =findViewById(R.id.buttonsignup2);
btnsignup.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Signup dosignup = new Signup(); // this is the Asynctask
        dosignup.execute("");
       }
   });
 }
protected void onStart() {
super.onStart();
//attaching value event listener
databaseUser.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        //clearing the previous artist list
        users.clear();
        //iterating through all the nodes
        for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
            //getting artist
            users user = postSnapshot.getValue(users.class);
            //adding artist to the list
            users.add(user);
        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});
}
   public class Signup extends AsyncTask<String, String, String> {
  String z = "";
  Boolean isSuccess = false;
   String username = addusername.getText().toString();
    String phone = addphone.getText().toString();
    String healthstatus = addhealthstatus.getText().toString();
@Override
protected String doInBackground(String... params) {
    if (username.trim().equals("") || phone.trim().equals("")) {
        z += "Please fill in all fields";
     } else {
        int count = 0;
        for (users user2 : users) {
            if (user2.getPhonenumber().equals(phone)) {
                count = 1;
            }
        }
        if (count == 0) {
            try {
                if(mFirstCheckBox.isChecked()) {
                    users user = new users(username, phone,"Diabetes");
                    databaseUser.child(phone).setValue(user);
                    addusername.setText("");
                    addphone.setText("");
                    addhealthstatus.setText("");
                }
                if(mSecondCheckBox.isChecked()) {
                    users user = new users(username, phone, "pressure");
                    databaseUser.child(phone).setValue(user);
                    addusername.setText("");
                    addphone.setText("");
                    addhealthstatus.setText("");
                }
                if(mThirdCheckBox.isChecked()) {
                    users user = new users(username, phone, 
                        healthstatus);
                    databaseUser.child(phone).setValue(user);
                    addusername.setText("");
                    addphone.setText("");
                    addhealthstatus.setText("");
                }
                z = "Account created";
                isSuccess = true;
            }
            catch (Exception ex) {
                z = "Mobile number was used";
                isSuccess = false;
            }
        }
        }
        return z;
      }
     }  
READ ALSO
I am facing error while using ImportFragment() method in my AndroidApplication

I am facing error while using ImportFragment() method in my AndroidApplication

I was building app where I used navigation drawer and fragmentsI created a test fragment layout and also its java file

68
importing project in android studio and generating .apk

importing project in android studio and generating .apk

I have a source code given by someone in which I have to make some changeswhen I generate

69
getFilter() on a custom ArrayAdapter not working

getFilter() on a custom ArrayAdapter not working

I am using a Custom ArrayAdapter to store User information for example sammy, robert, lizie are each one User objects and i am using a User type ArrayList to store all the User objects to ArrayList

234