Home > database >  Is there a way to setResources to XML file from firestore?
Is there a way to setResources to XML file from firestore?

Time:09-24

eventRole

I find out that the app can get the string array value in XML file via the getResource().getStringArray() method, but is there any way for me to get the string array value from Firestore itself and set it into the XML file? I want to get the data from "eventRole" and set it to an XML file. I hope I can found an answer to this, thank you.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name = "Roles">
        <item> </item>
    </string-array>

</resources>

CodePudding user response:

Yes, I want to get the array and populate it to my XML resource file as shown above.

You can't and shouldn't be writing data to your XML resource file. If you want to explicitly do it in a file, then you should consider using the external storage on the device.

If you only need to read the data from Firestore to display it in a view, simply use the following lines of code:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference eventRef = rootRef.collection("event");
eventRef.document("8B1shfn8Nwkvh39aRnEK").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                List<String> eventRole = (List<String>) document.get("eventRole");
                for (String e : eventRole) {
                    Log.d("TAG", e);
                }
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

The result in the logcat will be:

event leader
event helper
site supervisor

If you need to persist this data across your entire application then you should also consider using SharedPreferences.

  • Related