单击按钮时如何向Firebase数据库发送多个复选框值

时间:2018-03-16 07:14:16

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

我想向Firebase数据库发送多个复选框值。我用XML和一个Button取了三个Checkbox。我想在单击按钮时将选定的复选框文本存储到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数据库中实现的,但没有得到。在“用户”和“值(1,2,3)”之间必须有user_Id。图中缺少它 因为我在Firebase中输入了它们

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

请指导我,我错了。如果我完全错了,请告诉我任何其他替代方法。

提前致谢。

2 个答案:

答案 0 :(得分:1)

首先,当您使用此String user_id = mAuth.getCurrentUser().getUid(); 时,您将获得被点击的按钮的ID,而不是您选中的复选框的ID。其次,在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);
    }
});

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

public function index(Request $request)
{
    $view = $request['view'] ? $request['view'] :'grid'; 
    $purpose =  $request['purpose'] ? $request['purpose'] : 'rent'; 
    $sort =  $request['sort'] ? $request['sort'] : 'asc'; 

    $properties = $properties->where('purpose' , $purpose);
    if($sort == 'asc');
        $properties = $properties->sortBy('price');
     else 
         $properties = $properties->sortByDesc('price');
    $properties = $properties->paginate(5);

    return view('frontend.properties.index', [ 'view'=>$view , 'properties' => $properties , 'request'=> $request->all()  ]);
}

答案 1 :(得分:0)

 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 .