In my Android application, I have a MainActivity as the main base where there is a button labeled "Add Client Details." Upon clicking this button, the user should navigate to a new activity called AddViewClientDetailsActivity. I am focused on optimizing the application's size and usability, so I have decided to utilize fragments instead of activities where possible.

In the AddViewClientDetailsActivity, instead of using a separate fragment, I want to display a TextView within the Fill_One.java fragment. This TextView should dynamically show the name of the last inserted collection from Firestore when the user clicks on the "Add Client Details" button. The TextView will serve as the client ID, and I will also incorporate an image alongside it.

Current Status: I have successfully implemented the navigation to the AddViewClientDetailsActivity upon clicking the "Add Client Details" button. However, I am facing challenges in dynamically updating the TextView with the last inserted collection name within the Fill_One.java fragment.

Request for Assistance: I need guidance on how to efficiently update the TextView in the Fill_One.java fragment with the last inserted collection name retrieved from Firestore upon clicking the "Add Client Details" button. Additionally, suggestions on incorporating an image alongside the TextView for visual representation would be greatly appreciated.

  • Firebase Firestore database integration
  • Button to trigger the retrieval process
  • Display the last inserted collection name in a TextView in the next activity's fragment

Current Progress: I have created the button and integrated Firebase Firestore into my project. However, I am struggling with implementing the functionality to retrieve the last inserted collection name.

Code Snippet (not working): `// Code snippet where I attempted to retrieve the last inserted collection name private void fetchLastInsertedCollectionName() { FirebaseFirestore db = FirebaseFirestore.getInstance();

db.collection("collection_creation_times")
        .orderBy("creation_time", Query.Direction.DESCENDING)
        .limit(1)
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        String lastCreatedCollectionName = document.getId();
                        // Now I need to set this name in a TextView in the next activity's fragment
                        // How can I achieve this?
                    }
                } else {
                    Toast.makeText(getContext(), "Failed to fetch last inserted collection name", Toast.LENGTH_SHORT).show();
                }
            }
        });

} `

I need assistance in setting the retrieved last inserted collection name in a TextView in the next activity's fragment. Can anyone provide guidance or suggest the best approach to achieve this functionality?

I tried fetching the last inserted collection name from Firebase Firestore using a query. I expected to retrieve the name of the last inserted collection and set it in a TextView within a fragment of the next activity. However, I encountered difficulties in implementing this functionality and was seeking assistance to achieve the desired outcome.

enter image description here

enter image description here

1

There are 1 best solutions below

5
Alex Mamo On

There is no way to list the top-level collection or the sub-collections of a document with mobile SDKs. Since this operation isn't possible, so you cannot which collection was added last. What you can do instead, you can add in the Realtime Database, for example, a node that contains a list of collections. Add also a timestamp when you write a name in the database. To get the last collection name, order the elements descending as describes in the following post:

Checking your code, I can tell you for sure that you're looking to know the last document added and not the last collection added. If that's the case, then you should simply set the document ID inside the callback:

db.collection("collection_creation_times")
        .orderBy("creation_time", Query.Direction.DESCENDING)
        .limit(1)
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        String lastCreatedCollectionName = document.getId();
                        TextView tv = findViewById<TextView>(R.id.tv); //
                        tv.setText(lastCreatedCollectionName); //
                    }
                } else {
                    Toast.makeText(getContext(), "Failed to fetch last inserted collection name", Toast.LENGTH_SHORT).show();
                }
            }
        });

Because you're limiting the results to one, you'll get the last document ID displayed in the TextView.