1

Trying to get layout to change when my array has data inputted in it, currently it only displays the buttons even when there data inside the arraylist. Here I am trying to call the eventchange if it has the correct conditions. Currently it does not work and it still just displays the buttons. I dont know what it is missing right now hopefully anyone can help thank you.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);
    Button addMed = (Button) findViewById(R.id.add_medications_btn);
    Button addHM = (Button) findViewById(R.id.add_measurements_btn);
    rootAuthen = FirebaseAuth.getInstance();

    recyclerView = findViewById(R.id.recyclerViewHome);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    myArrayList = new ArrayList<medication_info>();
    myAdapter = new myHomeAdpater(home_page.this, myArrayList);
    EventChangeListener();
    recyclerView.setAdapter(myAdapter);
    if (myArrayList.size() == 0) {
        recyclerView.setVisibility(View.GONE);
        addMed.setVisibility(View.VISIBLE);
        addHM.setVisibility(View.VISIBLE);
        EventChangeListener();
    }
    else if(myArrayList.size() > 0)
    {
        recyclerView.setVisibility(View.VISIBLE);
        addMed.setVisibility(View.GONE);
        addHM.setVisibility(View.GONE);
        EventChangeListener();
    }
}

private void EventChangeListener() {
    FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    fstore.document("users/"+currentFirebaseUser.getUid()).collection("New Medications")
            .orderBy("Medication", Query.Direction.ASCENDING).addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
            if(e != null)
            {
                if(progressDialog.isShowing())
                    progressDialog.dismiss();
                Log.e("Firestore error", e.getMessage());
                return;
            }
            for(DocumentChange dc : queryDocumentSnapshots.getDocumentChanges())
            {
                if(dc.getType() == DocumentChange.Type.ADDED) {
                    medication_info m = dc.getDocument().toObject(medication_info.class);
                    m.setId(dc.getDocument().getId());
                    myArrayList.add(m);

                }
                myAdapter.notifyDataSetChanged();
                if(progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
            }
  • 1
    Can you post the entire code of your class? I feel like this is more an issue of not calling this method again after updating the list – Sergio Pardo Sep 10 '21 at 17:41
  • You cannot simply use the value of `myArrayList` outside the callback. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a custom callback or using Kotlin Coroutines. – Alex Mamo Sep 11 '21 at 08:03

0 Answers0