0

So when I log the results of array.size during the database query, it shows the correct size (At this moment it should be size = 2). After the methods are called and the following line Throws an index out of bounds exception, invalid index 0, size is 0: currentVehicle = dataVehicles.get(vehicleSelectorAdapter.getVehicleCurrentPos());

Am I missing something where the ArrayList is being cleared?

On Create Method

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_virtual_garage);

        mAuth = FirebaseAuth.getInstance();
        db = FirebaseFirestore.getInstance();
        vehicleInformation = new ArrayList<String>();

        dataVehicles = new ArrayList<Vehicle>();
        getVehicles();

        vehicleSelectorView = (RecyclerView) findViewById(R.id.virtualGarageSelector);
        vehicleSelectorLayout = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        vehicleSelectorView.setLayoutManager(vehicleSelectorLayout);
        vehicleSelectorAdapter = new VehicleAdapter(dataVehicles);
        vehicleSelectorView.setAdapter(vehicleSelectorAdapter);

        currentVehicle = dataVehicles.get(vehicleSelectorAdapter.getVehicleCurrentPos());
        Log.d("d", "VIN: "+currentVehicle.getVin());

Query to the Firestore DB

 public void getVehicles()
    {
        CollectionReference vehicleRef = db.collection("Vehicles");

        Query query = null;
        if(lastQueriedDocument != null)
        {
            query = vehicleRef.whereEqualTo("userID", FirebaseAuth.getInstance().getCurrentUser().getUid()).startAfter(lastQueriedDocument);
        } else {
            query = vehicleRef.whereEqualTo("userID", FirebaseAuth.getInstance().getCurrentUser().getUid());
        }


        query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if(task.isSuccessful())
                {
                    for(QueryDocumentSnapshot document: task.getResult())
                    {
                        Vehicle vehicle = document.toObject(Vehicle.class);
                        dataVehicles.add(vehicle);
                        Log.d("d", "Vehicle Added, Array size = "+dataVehicles.size());
                    }

                    if(task.getResult().size() != 0)
                    {
                        lastQueriedDocument = task.getResult().getDocuments().get(task.getResult().size()-1);
                    }
                    vehicleSelectorAdapter.notifyDataSetChanged();
                    Log.d("d", "Vehicle Added, Array size = "+dataVehicles.size());
                }
            }
        });
    }

EDIT: The following article resolved my issues:

Wait until Firestore data is retrieved to launch an activity

Thank you all for the help and direction!

  • `getVehicles();` is asynchronous `dataVehicles` will not be populated until after `addOnCompleteListener` has been called – Scary Wombat Mar 12 '20 at 00:21
  • Where it is, is maybe OK, but maybe move `currentVehicle = dataVehicles.get(vehicleSelectorAdapter.getVehicleCurrentPos());` to within `addOnCompleteListener` – Scary Wombat Mar 12 '20 at 00:44
  • Ive done both of those and I'm still getting the same errors – T. Sullivan Mar 12 '20 at 01:23
  • after doing what Scary Wombat said you need to set adapter to recyclerview in the addOnComleteListener only other the other will be empty – Abhinav Chauhan Mar 12 '20 at 04:21
  • Please check the duplicates to see why do you have this behavior and how can you solve this using a custom callback. – Alex Mamo Mar 12 '20 at 09:49

1 Answers1

0

I think this is related with variable scope:

dataVehicles declared inside of onCreate method is not available outside the method. Maybe it's enough to remove this declaration...

As you do not have errors in getVehicles() method you probably have global variable dataVehicles as well and you are shadowing it in onCreate.

I found nice article about it here point 6.

I hope it will help!

vitooh
  • 4,132
  • 1
  • 5
  • 16