0

At the start of the app, I want to download the database from Firebase and create array lists that will be used by different activities. From Jeff Gilfelt answer at Android global variable I learned how to make global variable available to all activities. But I can't figure out how to use this to make an arraylist from Firebase Database a global variable. I tried it this way, as described on the other question on making global variable:

public class FirstFirebaseCall extends Application {

private ArrayList<ItemAndPrice> listGroceryAndPrice = new ArrayList<>();
private final FirebaseDatabase database = FirebaseDatabase.getInstance();
private final DatabaseReference dbf = database.getReference("Accounts");
private final FirebaseUser mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();

@Override

public void onCreate() {
    super.onCreate();

    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    
    ChildEventListener childEventListener = dbf.child(Objects.requireNonNull(mCurrentUser).getUid()).child("Inventory")
            .addChildEventListener(new ChildEventListener() {

                ItemStockInitialSellingPrice itemInventory = new ItemStockInitialSellingPrice();

                @Override
                public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                    itemInventory = dataSnapshot.getValue(ItemStockInitialSellingPrice.class);

                    String item = Objects.requireNonNull(itemInventory).getItem();
                    double price = itemInventory.getSellingPrice();
                    ItemAndPrice itemAndPrice = new ItemAndPrice(item, price);

                    listGroceryAndPrice.add(itemAndPrice);

It did not worked out and displayed the error:

IllegalStateException: Default FirebaseApp is not initialized in this process com.example.storehelper. Make sure to call FirebaseApp.initializeApp(Context) first.

Where should I call FirebaseApp.initializeApp(Context) in my code?

Jo Den
  • 23
  • 6
  • 2
    "I can't figure out" is typically hard to help with, because we don't know where you're struggling. If you can show us the code of where you are stuck, there's a much better chance. That said: given that you mention global variables, I recommend reading my answer here: https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Sep 27 '20 at 04:04
  • Hi Frank van Puffelen. I edited my question. Please have a look. Thanks. – Jo Den Sep 27 '20 at 05:19
  • Hi @Frank van Puffelen, I placed this in onCreate and it seemed to work. I'm able to retrieve the list on other activity. Do you thinks this solution is ok? `FirebaseApp.initializeApp(this); FirebaseDatabase.getInstance().setPersistenceEnabled(true); FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference dbf = database.getReference("Accounts"); FirebaseUser mCurrentUser = FirebaseAuth.getInstance().getCurrentUser()` – Jo Den Sep 27 '20 at 05:49
  • Good to hear that you got it working! – Frank van Puffelen Sep 27 '20 at 15:07

0 Answers0