Store checkbox values in firebase using android studio











up vote
0
down vote

favorite












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;
}
}









share|improve this question
























  • What is the problem you're facing with this code? Also if it is something related to your database then do attach your database structure too.
    – PradyumanDixit
    Nov 11 at 3:07















up vote
0
down vote

favorite












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;
}
}









share|improve this question
























  • What is the problem you're facing with this code? Also if it is something related to your database then do attach your database structure too.
    – PradyumanDixit
    Nov 11 at 3:07













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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;
}
}









share|improve this question















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;
}
}






java android firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 18:54









Frank van Puffelen

221k25362387




221k25362387










asked Nov 10 at 18:10









user10633308

1




1












  • What is the problem you're facing with this code? Also if it is something related to your database then do attach your database structure too.
    – PradyumanDixit
    Nov 11 at 3:07


















  • What is the problem you're facing with this code? Also if it is something related to your database then do attach your database structure too.
    – PradyumanDixit
    Nov 11 at 3:07
















What is the problem you're facing with this code? Also if it is something related to your database then do attach your database structure too.
– PradyumanDixit
Nov 11 at 3:07




What is the problem you're facing with this code? Also if it is something related to your database then do attach your database structure too.
– PradyumanDixit
Nov 11 at 3:07












1 Answer
1






active

oldest

votes

















up vote
0
down vote













To get the selected checked values you can use Switch case following code



public void onCheckboxClicked(View view) {



    boolean checked = ((CheckBox) view).isChecked();

switch(view.getId()) {
case R.id.checkBox1:
....


break;
case R.id.checkBox2:
......

break;

case R.id.checkBox3:
......

break;`





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241959%2fstore-checkbox-values-in-firebase-using-android-studio%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    To get the selected checked values you can use Switch case following code



    public void onCheckboxClicked(View view) {



        boolean checked = ((CheckBox) view).isChecked();

    switch(view.getId()) {
    case R.id.checkBox1:
    ....


    break;
    case R.id.checkBox2:
    ......

    break;

    case R.id.checkBox3:
    ......

    break;`





    share|improve this answer



























      up vote
      0
      down vote













      To get the selected checked values you can use Switch case following code



      public void onCheckboxClicked(View view) {



          boolean checked = ((CheckBox) view).isChecked();

      switch(view.getId()) {
      case R.id.checkBox1:
      ....


      break;
      case R.id.checkBox2:
      ......

      break;

      case R.id.checkBox3:
      ......

      break;`





      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        To get the selected checked values you can use Switch case following code



        public void onCheckboxClicked(View view) {



            boolean checked = ((CheckBox) view).isChecked();

        switch(view.getId()) {
        case R.id.checkBox1:
        ....


        break;
        case R.id.checkBox2:
        ......

        break;

        case R.id.checkBox3:
        ......

        break;`





        share|improve this answer














        To get the selected checked values you can use Switch case following code



        public void onCheckboxClicked(View view) {



            boolean checked = ((CheckBox) view).isChecked();

        switch(view.getId()) {
        case R.id.checkBox1:
        ....


        break;
        case R.id.checkBox2:
        ......

        break;

        case R.id.checkBox3:
        ......

        break;`






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 5:05

























        answered Nov 11 at 3:46









        Padma Priya

        105




        105






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241959%2fstore-checkbox-values-in-firebase-using-android-studio%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ