【问题标题】:How to send multiple checkbox values to Firebase Database when a Button is clicked Android单击按钮时如何将多个复选框值发送到 Firebase 数据库 Android
【发布时间】:2018-08-25 04:27:55
【问题描述】:

我想将多个复选框值发送到 Firebase 数据库。我拿了三个 XML 中的复选框和一个按钮。当单击按钮并转到另一个活动时,我想将选定的复选框文本存储到 Firebase。这是我的代码,但它不起作用。

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

        }
    });

这是我想在 Firebase 数据库中实现的,但没有得到。在 'Users' 和 'Values(1,2,3)' 之间必须有 user_Id。图中没有 因为我已经在 Firebase 中输入了它们

请指导我,我哪里出错了。如果我完全错了,请告诉我任何其他方法来完成这项工作。

提前致谢。

【问题讨论】:

  • 那你得到了什么?
  • Firebase 中没有任何内容。单击按钮时将进入下一个活动
  • 我为这个应用设置了登录功能。我正在这样做,登录到应用程序后
  • @ThrinadhReddy firebase 中的任何内容都不会被存储,因为您在 switch case 中获得的 id 是按钮,即您设置了 onclick 的 saveButton,但您正在检查 id 是否属于哪个用户的复选框已检查。只需从按钮 onclick 的匿名实现中删除 switch case 并检查哪个复选框处于选中状态,哪个未选中,根据该复选框,您可以通过 firebase 发送数据
  • 谢谢...它帮助了我...@Lucky

标签: java android firebase firebase-realtime-database android-checkbox


【解决方案1】:

首先,当您使用此view.getId() 时,您将获得被点击的按钮的 ID,而不是被选中的复选框的 ID。其次,没有必要在你的 switch 语句的每种情况下都使用以下代码行:

String user_id = mAuth.getCurrentUser().getUid();

只用一次就足够了。事实上,根本不需要使用 switch 语句。您的代码应如下所示:

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

【讨论】:

    【解决方案2】:
     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 .
    

    【讨论】:

    • 是的,它们只有真假。我在“如果”条件下写了它。如果是真的,我想在 Firebase 数据库中保存一些值
    猜你喜欢
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 2013-08-09
    • 2020-11-09
    相关资源
    最近更新 更多