我在更新存储桶(键)上的对象时遇到了一些困难。在持久性时,我将对象列表(在本例中为 Trip)push()
到节点。后来,我检索了它,并在点击按钮时进行了更新(在本例中为更新出价信息)。我似乎找不到一种简单的方法来做到这一点,除非我之前推送行程时,我必须手动调用getkey()
然后将自动生成的 uid 更新为对象 Trip 本身。有什么想法可以轻松完成吗?谢谢
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);
}
});
}
};
我不太确定您要完成什么(相关 JSON 的片段 - 作为文本,而不是屏幕截图 - 会有所帮助)。但我最好的猜测是你正在寻找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);
}
});
}
FirebaseRecylerAdapter 有一个名为 parseSnapShot 的覆盖,你可以像这样挂上它。我不知道更好的解决方案 - 所以希望有人能提供更好的解决方案。
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);
}
});
}
};