Java 从firebase获取列表时,特定值仅显示在一个recyclerview项目中,而不显示在列表中显示的其他项目中
我已经成功地从firebase中显示了一个列表,其中显示了出价以及距离和持续时间。firebase数据库中存储了两个位置,我使用这些位置使用GoogleMapsAPI计算距离和持续时间。我也能成功地获得距离和持续时间 问题是: 该列表显示recyclerview列表中的所有出价,但距离和持续时间仅显示在列表的一个项目上最新的一个项目上,所有出价都正确显示 我还尝试过在覆盖方法“taskcompleted”中添加bidlist.add(bids),但另一个问题是recyclerview多次显示相同的元素 守则:Java 从firebase获取列表时,特定值仅显示在一个recyclerview项目中,而不显示在列表中显示的其他项目中,java,android,firebase,firebase-realtime-database,android-recyclerview,Java,Android,Firebase,Firebase Realtime Database,Android Recyclerview,我已经成功地从firebase中显示了一个列表,其中显示了出价以及距离和持续时间。firebase数据库中存储了两个位置,我使用这些位置使用GoogleMapsAPI计算距离和持续时间。我也能成功地获得距离和持续时间 问题是: 该列表显示recyclerview列表中的所有出价,但距离和持续时间仅显示在列表的一个项目上最新的一个项目上,所有出价都正确显示 我还尝试过在覆盖方法“taskcompleted”中添加bidlist.add(bids),但另一个问题是recyclerview多次显示相同
getUserFromLoc(users_ref, delivery_id, new UserFromLocationCallback() {
@Override
public void userFromLocation(LatLng loc) {
order_bids.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.e("datasnap",""+dataSnapshot);
for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
Bids bids = new Bids();
Log.e("datasnap",""+dataSnapshot1);
if(dataSnapshot1.child(delivery_id).child("location_lat").getValue()!=null){
double d = (Double) dataSnapshot1.child(delivery_id).child("location_lat").getValue();
double lat=Double.parseDouble(dataSnapshot1.child(delivery_id).child("location_lat").getValue().toString());
double lng=Double.parseDouble(dataSnapshot1.child(delivery_id).child("location_lng").getValue().toString());
LatLng latLng = new LatLng(lat,lng);
distance_task.getDirectionsUrl(loc, latLng,getString(R.string.google_maps_key),"driving");
bids.setTruck_type(deliveryInfo.getType_of_truck());
bids.setDelivery_id(deliveryInfo.getDeliveryID());
bid_amt=dataSnapshot1.child(delivery_id).child("bid_value").getValue().toString();
Log.e(TAG, "taskCompleted: "+ dataSnapshot1.child(delivery_id).child("bid_value").getValue().toString());
bid_amt=dataSnapshot1.child(delivery_id).child("bid_value").getValue().toString();
Log.e("Bids data",""+bids.getBid());
bids.setBid(bid_amt);
bids.setDistance(getDistance(loc, latLng)[0]);
bids.setDuration(getDistance(loc, latLng)[1]);
bidslist.add(bids);
distance_task.setLoadListener(new CalculateDistanceTime.taskCompleteListener() {
@Override
public void taskCompleted(String[] time_distance) {
Log.e("Duration truck","" + time_distance[1]);
Log.e("distance truck","" + time_distance[0]);
truck_distance=time_distance[0];
truck_duration=time_distance[1];
bids.setDuration(truck_duration);
bids.setDistance(truck_distance);
initRecyclerView(bidslist, new BidsClickInterface() {
@Override
public void bidsCLick(View view, int pos) {
Toast.makeText(BidsActivity.this,"click:"+pos,Toast.LENGTH_SHORT).show();
}
},BidsActivity.this);
Log.e(TAG, "Bids_size "+bidslist.size());
}
});
Log.e("datasnap2","gg1"+d);
}else {
Log.e("datasnap2","gg2");
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.e("error:",""+databaseError.getMessage());
}
});
}
});
我知道这是一个相当模糊的问题,但仍然请帮助我,因为我在这个问题上被困了相当长的时间问题是您为每个
dataSnapshot1
调用initRecyclerView
,因此,只有最新的出价才传递给recyclerview适配器。(这是因为您在for循环内调用了initRecyclerView
),而不是在for循环后调用initRecyclerView
getUserFromLoc(users\u ref,delivery\u id,new UserFromLocationCallback(){
@凌驾
public void userFromLocation(LatLng loc){
order\u bids.addValueEventListener(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
Log.e(“datasnap”和“+dataSnapshot”);
对于(DataSnapshot dataSnapshot1:DataSnapshot.getChildren()){
投标=新投标();
Log.e(“datasnap”,“dataSnapshot1”);
if(dataSnapshot1.child(delivery\u id).child(“location\u lat”).getValue()!=null){
double d=(double)dataSnapshot1.child(delivery_id).child(“location_lat”).getValue();
double-lat=double.parseDouble(dataSnapshot1.child(delivery\u-id).child(“location\u-lat”).getValue().toString());
double lng=double.parseDouble(dataSnapshot1.child(delivery_id).child(“location_lng”).getValue().toString());
LatLng LatLng=新LatLng(lat,lng);
距离任务。getDirectionsUrl(loc,latLng,getString(R.string.google地图键),“驾驶”);
bids.setTruck_type(deliveryInfo.getType_of_truck());
bids.setDelivery_id(deliveryInfo.getDeliveryID());
bid_amt=dataSnapshot1.child(delivery_id).child(“bid_value”).getValue().toString();
Log.e(标记“taskCompleted:”+dataSnapshot1.child(delivery_id).child(“bid_value”).getValue().toString());
bid_amt=dataSnapshot1.child(delivery_id).child(“bid_value”).getValue().toString();
Log.e(“Bids数据”,“Bids.getBid());
投标文件。收标日期(投标金额);
投标。设定距离(getDistance(loc,latLng)[0]);
投标。设定工期(getDistance(loc,latLng)[1]);
标书列表。添加(标书);
距离\u任务.setLoadListener(新的CalculateInstanceTime.taskCompleteListener(){
@凌驾
公共无效任务已完成(字符串[]时间\距离){
Log.e(“卡车持续时间”,“时间+距离[1]);
Log.e(“卡车距离”,“时间+距离[0]);
卡车距离=时间距离[0];
卡车持续时间=时间距离[1];
投标。设置工期(卡车工期);
投标。设置距离(卡车距离);
}
});
Log.e(“datasnap2”、“gg1”+d);
}否则{
Log.e(“datasnap2”、“gg2”);
}
}
//在for循环之后调用initRecyclerView
initRecyclerView(bidslist,新BidsClickInterface(){
@凌驾
公共无效bidsCLick(视图,int pos){
Toast.makeText(BidsActivity.this,“单击:”+pos,Toast.LENGTH_SHORT.show();
}
},双向性。这);
Log.e(标记“Bids_size”+bidslist.size());
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
Log.e(“错误:”,“+databaseError.getMessage());
}
});
}
});
我遵照您的建议,在for循环之后调用了initRecyclerView()
,但现在我无法在任何recyclerview项目中获得距离和持续时间。这是不是因为taskCompleted回调中定义了bids.setDistance()
。@AryanSoni是的,这似乎是个问题,但不幸的是,我找不到合适的方法来解决它。很抱歉