firebase, how to update data at a key

2019-07-08 01:48发布

问题:

I have some difficulties in updating an object at a bucket (key). At the time of persistence, I push() a list of objects (in this case, Trip) to a node. Later, I retrieved it, and make an update (in this case, updating the bid info) when a button is clicked. I can't seem to find an easy way to do that, unless when I had pushed the trip before, I had to manually call getkey() and then update the auto-generated uid to the object Trip itself. Any idea to get it done easily? thanks

mFirebaseAdapter = new FirebaseRecyclerAdapter<Trip,
            TripViewHolder>(
            Trip.class,
            R.layout.item_message,
            TripViewHolder.class,
            mFirebaseDatabaseReference.child(CHILD_TRIPS)) {

        @Override
        protected void populateViewHolder(TripViewHolder viewHolder, final Trip model, int position) {

            viewHolder.btnTakeNow.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {

                     Bid bidAtReserved = new Bid(mDriverCarInfo, model, MY_PRICE);

                     mFirebaseDatabaseReference.child(CHILD_TRIPS)
                             //here, I want to update the bidding to this trip, but 
                             //I can't get the uid of the trip, unless I had 
                             //manually recorded at the time of pushing the trip
                             .child(model.getUid()) 
                              .child("bids").push().setValue(bidAtReserved);


                 }
            });
        }
    };

回答1:

I'm not really sure what you're trying to accomplish (a snippet of the relevant JSON - as text, not a screenshot - would be helpful). But my best guess is that you're looking for getRef().

protected void populateViewHolder(TripViewHolder viewHolder, final Trip model, final int position) {
    viewHolder.btnTakeNow.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mFirebaseAdapter.getRef(position).child("price").setValue(MY_PRICE);
        }
    });
}


回答2:

The FirebaseRecylerAdapter has an override called parseSnapShot, you can hook onto that like this. I´m not aware of an better the solution - so hopefully someone can provide better.

mFirebaseAdapter = new FirebaseRecyclerAdapter<Trip,
                TripViewHolder>(
                Trip.class,
                R.layout.item_message,
                TripViewHolder.class,
                mFirebaseDatabaseReference.child(CHILD_TRIPS)) {

        @Override
        protected Trip parseSnapshot(DataSnapshot snapshot) {

            Trip trip = super.parseSnapshot(snapshot);
            trip.setDbKey(snapshot.getKey());

            return trip;
        }

        @Override
        protected void populateViewHolder(TripViewHolder viewHolder, final Trip model, int position) {

            viewHolder.btnTakeNow.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {

                     Bid bidAtReserved = new Bid(mDriverCarInfo, model, MY_PRICE);

                     mFirebaseDatabaseReference.child(CHILD_TRIPS)
                             //here, I want to update the bidding to this trip, but I can't get the uid of the trip, unless I had manually recorded at the time of pushing the trip
                             .child(model.getUid()) 
                              .child("bids").push().setValue(bidAtReserved);


                 }
            });
        }
    };