Palash Dey
Palash Dey

Reputation: 84

Firebase database never update my value in specific child

I am trying to update the value in a specific child using code below. But it does not update the value instead it creates new node and insert this value under the node.Please check the code and firebase structure picture below:

  referenceUpdatedLand=FirebaseDatabase.getInstance().getReference("Land_Info");

                Query queryUpdated =referenceUpdatedLand.orderByChild("Khatian").equalTo(landkhatin);

                Toast.makeText(VehicleBookedLandActivity.this, ""+landkhatin, Toast.LENGTH_SHORT).show();


                queryUpdated.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren())

                        {
                            int vehicleaviable= Integer.parseInt(avaiablecount);

                            String land_vehicle_type_aviable ="land_bike_available";
                            nowVehicleAvai=vehicleaviable-1;

                            String nowVehicleTypeAvaiable= String.valueOf(nowVehicleAvai);



                            HashMap<String, Object> map = new HashMap<>();
                            //map.put(land_vehicle_type_aviable,nowVehicleTypeAvaiable );
                            referenceUpdatedLand.child(land_vehicle_type_aviable).setValue(nowVehicleTypeAvaiable);

                            Intent intent = new Intent(getApplicationContext(), VehicleNavActivity.class);
                            startActivity(intent);
                            finish();


                            Toast.makeText(getApplicationContext(), "Sucessfull", Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

Below showing the images of firebase:

firebase_wanted_change_value_here.jpg

instead_change_value_here.jpg

Note: I also using updatechildren method but it erases my all Land_Info data and then adds this value as a new reference

                map.put(land_vehicle_type_aviable,nowVehicleTypeAvaiable );

                            referenceUpdatedLand.updateChildren(map);

Upvotes: 0

Views: 71

Answers (1)

Jignesh Mayani
Jignesh Mayani

Reputation: 7193

I think there is a mistake in your update query,Please make below change and try:

referenceUpdatedLand=FirebaseDatabase.getInstance().getReference("Land_Info");

                Query queryUpdated =referenceUpdatedLand.orderByChild("Khatian").equalTo(landkhatin);

                Toast.makeText(VehicleBookedLandActivity.this, ""+landkhatin, Toast.LENGTH_SHORT).show();


                queryUpdated.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren())

                        {
                            int vehicleaviable= Integer.parseInt(avaiablecount);

                            String land_vehicle_type_aviable ="land_bike_available";
                            nowVehicleAvai=vehicleaviable-1;

                            String nowVehicleTypeAvaiable= String.valueOf(nowVehicleAvai);



                            HashMap<String, Object> map = new HashMap<>();
                            //map.put(land_vehicle_type_aviable,nowVehicleTypeAvaiable );
                            referenceUpdatedLand.child(dataSnapshot1.getKey())child(land_vehicle_type_aviable).setValue(nowVehicleTypeAvaiable); //change

                            Intent intent = new Intent(getApplicationContext(), VehicleNavActivity.class);
                            startActivity(intent);
                            finish();


                            Toast.makeText(getApplicationContext(), "Sucessfull", Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

Upvotes: 1

Related Questions