无法从 Firebase 数据库中检索 Arraylist 值

时间:2021-03-23 13:26:27

标签: java android firebase firebase-realtime-database arraylist

我试图将数据从 firebase 实时数据库提取到我的 android 应用程序,并成功地在 ondatachange 函数中接收数据,但是当我尝试将值放入数组列表时,我失败了。我试图打印之前在 logcat 中的值,但在收到一个空白列表后,我尝试将函数设置为返回一个数组列表并将一个新变量设置为返回的 valuea,但仍然得到一个空数组。我能否得到这方面的帮助,因为我打算在快照功能之外操作数据。

下面是我的代码:

''' 公共类 MapsActivity 扩展 FragmentActivity 实现 OnMapReadyCallback {

private static final String TAG = "MapsActivity";
private GoogleMap mMap;
private Geocoder geocoder;
private int ACCESS_LOCATION_REQUEST_CODE = 10001;
FusedLocationProviderClient fusedLocationProviderClient;
LocationRequest locationRequest;

Marker userLocationMarker;
Circle userLocationAccuracyCircle;
public static FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
public static DatabaseReference mReferenceCrossings = mDatabase.getReference();
public static ArrayList<Location> results = new ArrayList<Location>();
String reference = "Crossings";
String orderKey = "Latitudes";


public ArrayList<Location> sortedQuery() throws InterruptedException {

    return results;


}


interface FirebaseCallback {

    void onCallback(ArrayList<Location> locations);
}
private  ArrayList<Location> readData(FirebaseCallback firebaseCallback) {
    ArrayList<Location> results = new ArrayList<Location>();
    DatabaseReference targetRef = mReferenceCrossings.child(reference);
    targetRef.orderByChild(orderKey).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot datasnapshot) {
            for (DataSnapshot child : datasnapshot.getChildren()) {
                Log.d("datavalues", "cut" + datasnapshot);
                double latitude = Double.parseDouble(child.child("Latitude").getValue().toString());
                double longitude = Double.parseDouble(child.child("Longitude").getValue().toString());
                Location entry = new Location("coordinates");
                entry.setLatitude(latitude);
                entry.setLongitude(longitude);
                results.add(entry);
            }

            firebaseCallback.onCallback(results);
            Log.d("Monket", String.valueOf(results));
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Log.d("Firebase error", error.getDetails());
        }

    });
    return results;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    geocoder = new Geocoder(this);
    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    locationRequest = LocationRequest.create();
    locationRequest.setInterval(330);
    locationRequest.setFastestInterval(330);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    Intent intentBackgroundService = new Intent(this, FirebasePushNotification.class);
    startService(intentBackgroundService);

   ArrayList<Location> crossings = readData(new FirebaseCallback() {
        @Override
        public void onCallback(ArrayList<Location> locations) {

        }
    });
    Log.d("crossings", String.valueOf(crossings));

'''

0 个答案:

没有答案