skip to Main Content

I am going to make a simple app and I am completely new to Android development. I want to develop an edit button to save my data in the Realtime Database. This is my code:

holder.edite.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       final DialogPlus dialogPlus=DialogPlus.newDialog(holder.title.getContext())
               .setContentHolder(new ViewHolder(R.layout.dialogcontent))
               .setExpanded(true,2100)
               .create();

       View myView=dialogPlus.getHolderView();
        EditText title=myView.findViewById(R.id.hTitle);
        EditText description=myView.findViewById(R.id.hDescription);
        Button submit=myView.findViewById(R.id.usubmit);

        title.setText(myItems.getName());
        description.setText(myItems.getAddHomeworkDescription());

        dialogPlus.show();

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String,Object> map=new HashMap<>();
                map.put("name",title.getText().toString());
                map.put("addHomeworkDescription",description.getText().toString());

                DatabaseReference myRef = getInstance().getReference().child("Homework");
                String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
//                String key = myRef.push().getKey();
                myRef.child(uid).child()
                        .updateChildren(map);
                dialogPlus.dismiss();
            }
        });
    }
});

This is how my firebase database looks like

enter image description here

What I want to add into .child() to get highlighted(In the image) Unique id direction. But this unique ID is not always same. It change everythime when user create new one.

2

Answers


  1. You will either have to know the push key (-NN...) value of the node you want to update already, determine it with a query, or loop over all child nodes and update them all.

    Update a specific child with a known push key

    myRef.child(uid).child("-NNsQO7O9lh0ShefShV")
                    .updateChildren(map);
    

    Update children matching a specific query

    Say that you know you want to update the node with name "test12", you can use a query for that:

    myRef.child(uid).orderByChild("name").equalToValue("test12").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot nodeSnapshot: dataSnapshot.getChildren()) {
                nodeSnapshot.getRef().updateChildren(map);
            }
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {
            throw databaseError.toException();
        }
    })
    

    Update all children

    This is a simpler version of the above, by removing the query condition:

    myRef.child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot nodeSnapshot: dataSnapshot.getChildren()) {
                nodeSnapshot.getRef().updateChildren(map);
            }
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {
            throw databaseError.toException();
        }
    })
    
    Login or Signup to reply.
  2. Try this code to update all information in firebase

    updateRef.child(uid).orderByChild("name").equalTo(myItems.getName())
                                .addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                for (DataSnapshot nodeSnapshot: dataSnapshot.getChildren()) {
                                    nodeSnapshot.getRef().child("name").setValue(map.get("name"));
                                    dialogPlus.dismiss();
                                }
                            }
                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                                throw databaseError.toException();
                            }
                        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search