单击按钮时如何将多个复选框值保存到Firebase

时间:2018-11-24 16:00:02

标签: java android firebase android-studio android-checkbox

这是HTML文件。我在将多个复选框的值保存到Firebase中时遇到麻烦。我想在单击按钮时将选定的复选框文本存储到Firebase,然后转到另一个活动。

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_marginTop="50dp">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/image"
                    android:layout_width="match_parent"
                    android:layout_height="180dp"
                    android:background="#FCFAFA"
                    android:scaleType="centerCrop" />

                <ImageView
                    android:id="@+id/camera"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:layout_gravity="right|bottom"
                    android:layout_marginBottom="24dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="8dp"
                    android:background="@drawable/button_bg_round"
                    android:padding="8dp"
                    android:tint="#858585"
                    app:srcCompat="@drawable/ic_camera"
                    tools:ignore="VectorDrawableCompat" />
            </FrameLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:paddingBottom="12dp"
                android:paddingLeft="12dp"
                android:paddingRight="12dp"
                android:paddingTop="4dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="12dp"
                    android:text="Place "
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <EditText
                        android:id="@+id/placeName"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@color/transparent"
                        android:drawablePadding="8dp"
                        android:gravity="center_vertical"
                        android:minHeight="40dp"
                        android:padding="8dp" />

                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="24dp"
                    android:text="Street"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <EditText
                        android:id="@+id/streetName"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@color/transparent"
                        android:drawablePadding="8dp"
                        android:gravity="center_vertical"
                        android:minHeight="40dp"
                        android:padding="8dp" />

                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="24dp"
                    android:text="Postcode"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <EditText
                        android:id="@+id/postcodeName"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@color/transparent"
                        android:drawablePadding="8dp"
                        android:gravity="center_vertical"
                        android:minHeight="40dp"
                        android:padding="8dp" />

                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="24dp"
                    android:text="City"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <EditText
                        android:id="@+id/cityName"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@color/transparent"
                        android:drawablePadding="8dp"
                        android:gravity="center_vertical"
                        android:minHeight="40dp"
                        android:padding="8dp" />

                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="24dp"
                    android:text="Facility"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="4dp"
                    android:orientation="horizontal">

                    <CheckBox
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/facility_ramp"
                        android:text="@string/ramp"/>

                    <EditText
                        android:id="@+id/desc_ramp"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Description"
                        android:layout_marginLeft="80dp"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="4dp"
                    android:orientation="horizontal">

                    <CheckBox
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/facility_lift"
                        android:text="@string/disabled_lift"/>

                    <EditText
                        android:id="@+id/desc_lift"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Description"
                        android:layout_marginLeft="38dp"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="4dp"
                    android:orientation="horizontal">

                    <CheckBox
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/facility_counter"
                        android:text="@string/counter"/>

                    <EditText
                        android:id="@+id/desc_counter"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Description"
                        android:layout_marginLeft="35dp"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="4dp"
                    android:orientation="horizontal">

                    <CheckBox
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/facility_toilet"
                        android:text="@string/toilet"/>

                    <EditText
                        android:id="@+id/desc_toilet"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Description"
                        android:layout_marginLeft="10dp"/>

                </LinearLayout>

            </LinearLayout>

            <Button
                android:id="@+id/addBtn"
                android:background="@drawable/searchbutton"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:text="CREATE PLACE"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"/>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

这是Java代码。我没有将选中的复选框项/日期保存到Firebase中,因为一旦选中复选框,我不知道用于存储设施描述的Java代码。帮忙,谢谢

    //declare
    mAddPlace = (EditText) findViewById(R.id.placeName);
    mAddStreet = (EditText) findViewById(R.id.streetName);
    mAddPostcode = (EditText) findViewById(R.id.postcodeName);
    mAddCity = (EditText) findViewById(R.id.cityName);
    mbtnAdd = (Button) findViewById(R.id.addBtn);

    mRampCheckBox = findViewById(R.id.facility_ramp);
    mCounterCheckBox = findViewById(R.id.facility_counter);
    mLiftCheckBox = findViewById(R.id.facility_lift);
    mToiletCheckBox = findViewById(R.id.facility_toilet);

    descRamp = findViewById(R.id.desc_ramp);
    descLift = findViewById(R.id.desc_lift);
    descCounter = findViewById(R.id.desc_counter);
    descToilet = findViewById(R.id.desc_toilet);


    mRampCheckBox.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (((CheckBox) v).isChecked()) {
                descRamp.setEnabled(true);

            }
            else
                descRamp.setEnabled(false);

        }
    });

    mLiftCheckBox.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (((CheckBox) v).isChecked()) {
                descLift.setEnabled(true);
            }
            else
                descLift.setEnabled(false);

        }
    });

    mCounterCheckBox.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (((CheckBox) v).isChecked()) {
                descCounter.setEnabled(true);
            }
            else
                descCounter.setEnabled(false);

        }
    });

    mToiletCheckBox.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (((CheckBox) v).isChecked()) {
                descToilet.setEnabled(true);
            }
            else
                descToilet.setEnabled(false);

        }
    });

1 个答案:

答案 0 :(得分:0)

    private boolean mRamp , mLift , mCounter , mToilet ;   
     private DatabaseReference UserRef;
     private FirebaseAuth mAuth;
    
    
     @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            mAuth = FirebaseAuth.getInstance();
            UserRef = FirebaseDatabase.getInstance().getReference().child("Users");
        
    
    
    mRampCheckBox.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    descRamp.setEnabled(true);
                    mRamp = true;
    
                }
                else
                    descRamp.setEnabled(false);
                    mRamp = false;
    
    
            }
        });
    
        mLiftCheckBox.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    descLift.setEnabled(true);
                    mLift = true;
                }
                else
                    descLift.setEnabled(false);
                    mLift  = false;
    
            }
        });
    
        mCounterCheckBox.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    descCounter.setEnabled(true);
                    mCounter = true;
                }
                else
                    descCounter.setEnabled(false);
                    mCounter = false;
    
            }
        });
    
        mToiletCheckBox.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    descToilet.setEnabled(true);
                    mToilet = true;
                }
                else
                    descToilet.setEnabled(false);
                    mToilet = false;
    
            }
        });
    

}
    
    
    
    private void uploadData()
    {
      HashMap userMap = new HashMap();
            userMap.put("mRamp" ,mRamp);
            userMap.put("mLift" ,mLift);
            userMap.put("mCounter" ,mCounter);
            userMap.put("mToilet" ,mToilet);
            
    
            UserRef.updateChildren(userMap).addOnCompleteListener(new OnCompleteListener()
            {
                @Override
                public void onComplete(@NonNull Task task)
                {
                    if(task.isSuccessful())
                    {
                      
                        Toast.makeText(this, "Data Saved", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                       
                        Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
                    }
                }
            });
    }

在您要上传数据的位置调用此方法 uploadData();