简体   繁体   中英

Particular Value is displayed in just one recyclerview item but not in other items displayed in a list while fetching list from firebase

I have successfully displayed a list from the firebase which displays the bids and the distance and duration it will take. Two locations are stored in firebase database and i use those locations to calculate distance and duration using google maps api. I am able to successfully get both distance and duration as well.

The problem: The list displays all the bids in a recyclerview list but the distance and duration is displayed on just one item of the list the most recent one, all bids are displayed correctly

Also i have tried adding bidlist.add(bids) in override method "taskcompleted" but another problem arises that the recyclerview displays same elements multiple times

the code:

 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());

                    }
                });
            }
        });

BidsActivity

I know that this is a pretty vague question but still please help me as i am stuck in this problem for quite long

The problem is you call initRecyclerView for each dataSnapshot1 , as a result, only the latest bid is passed to the recyclerview adapter. (this is because you have called initRecyclerView inside the for loop), Instead call initRecyclerView after the for loop.

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);

                            }

                        });

                        Log.e("datasnap2","gg1"+d);
                    }else {
                        Log.e("datasnap2","gg2");
                    }

                }
                // Call initRecyclerView after the for loop
                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());
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.e("error:",""+databaseError.getMessage());

            }
        });
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM