EN VI

Android - Is it possible to read data from firebase without an event listener?

2024-03-16 23:00:14
Android - Is it possible to read data from firebase without an event listener?

I have a simple android application that I created using xamarin. I want to retrieve information from a specific location in my data base whenever a user press a certain button. I'm using the real time database. Is this possible?

I searched online for a few hours but I only found information about how to retrieve data when the data changes, I just need to retrieve it when a button is pressed.

![The structure of my database for context][1]

I want to retrieve the strings from a specific ID when a user presses a certain button. How do I retrieve the strings?

EDIT: I followed the advice of one of the comments that provided me with a java code, I'm trying to create a c# equivalent that i can use in xamarin, this is what I have so far:

  btnSearch.Click += async (sender, e) =>
                {
                    DatabaseReference dbref = AppDataHelper.GetDatabase().GetReference("trip");
                    dbref.ValueChanged += HandleValueChanged;
                    void HandleValueChanged(object sender, ValueChangedEventArgs args)
                    {
                        if (args.DatabaseError != null)
                        {
                            System.Diagnostics.Debug.WriteLine(args.DatabaseError.Message);
                            return;
                        }

                    }
                };

ValueChanged and ValueChangedEventArgs aren't recognized, what should I do? [1]: https://i.stack.imgur.com/MrSDt.jpg

Solution:

You said:

I only found information about how to retrieve data when the data changes, I just need to retrieve it when a button is pressed.

From the Firebase documentation on listening for events (emphasis mine):

You can use the ValueChanged event to subscribe on changes of the contents at a given path. This event is triggered once when the listener is attached and again every time the data, including children, changes.

So when you attach a listener, it immediately gets the current value from the server and triggers your callback with that.


To read a value with a listener, you can use GetValueAsync. I'm not sure if the Xamarin library has that though, as it wasn't created by Firebase - as far as I know.

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login