I want to change the data in the Firebase database with an Android app. So I want it to be true when I open it, and false when I close it. (0-states-001-True/False) So when I press the toggle button, I want to update the data(true/false)). I shared the code below. How do I update data? These codes are not working, the data is wrong. database application

DatabaseReference dbProducts1 = FirebaseDatabase.getInstance().getReference("0").child("states").child("001");

dbProducts1.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot1) {

                    if(dataSnapshot1.exists()){

                        for(DataSnapshot productSnapshot : dataSnapshot1.getChildren()){
                            Product1 p1 = productSnapshot.getValue(Product1.class);
                            productList.add(p1);
                        }

                        adapter = new ProductsAdapter(MainActivity.this, productList);
                        recyclerView.setAdapter(adapter);

                    }

                }

public class Product1 extends Product {

            private boolean states;

            public Product1(){

            }

            public Product1(boolean states) {

                this.states = states;
            }


            public boolean getStates() {
                return states;
            }

        }

         public void onBindViewHolder(ProductViewHolder holder, int position) {
                Product product = productList.get(position);

                // holder.textViewID.setText(String.valueOf(product.getid()));
                holder.viewStatus.setText(String.valueOf(product.getStates()));

        }


class ProductViewHolder extends RecyclerView.ViewHolder {

                ToggleButton viewStatus;
                public ProductViewHolder(View itemView) {
                    super(itemView);

                    viewStatus = itemView.findViewById(R.id.toggle_Data);

        }
        }

2 Answers

0
Oshan Madusanka On

Try this to update when switch state changes

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();

DatabaseReference ref = mDatabase.child("states").child("001")

mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Map<String, Object> newStatus = new HashMap<>();
        childUpdates.put("states", isChecked)

        ref.update(newStatus)
    }
});
-1
Community On
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("status/001");
if(isChecked){

    myRef.setValue(true);

}else{
    myRef.setValue(false);

}
            }
        });